zl程序教程

您现在的位置是:首页 >  Python

当前栏目

在linux中Python环境变量的设置

2023-03-20 14:46:07 时间

本篇将为刚初学Python小白讲解在linux下Python环境变量的设置,两种方法分享给大家:

在linux中Python环境变量的设置

1.命令窗口添加路径

<p style="line-height: 1.75em"><span>export PYTHONPATH=$PYTHONPATH:/home/test_BugScan/libs<br /></span></p>

注意:此方法只在当前命令窗口生效,即如果打开一个新的Terminal 窗口,定位到当前目录, 打印PYTHONPATH 是没有刚才加入的路径的。

2.在python 中添加

<p style="line-height: 1.75em"><span>import sys
sys.path.append('/home/test_BugScan/libs/')<br /></span></p>

举例:

将windows中的代码同步到Linux中,文件等级:

BugScan

-test

– -test_units.py

… …

-libs

– -units.py

… ….

test_units.py

<p style="line-height: 1.75em"><span>import unittest
from libs.units import  *

class TestFunc(unittest.TestCase):
    pass
# 在执行文件时出现异常
>> ImportError: No module named libs<br /></span></p>

解决方法:

<p style="line-height: 1.75em"><span>export PYTHONPATH=$PYTHONPATH:/home/test_BugScan/<br /></span></p>
<p style="line-height: 1.75em"><span># 执行成功
python -m unittest test_utils.TestFunc<br /></span></p>

两种方法都非常好用,希望能帮助到初学Python的小白。