在linux中Python环境变量的设置

327次阅读
没有评论

本篇将为刚初学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的小白。

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python基础教程2022-12-07发表,共计590字。
新手QQ群:570568346,欢迎进群讨论 Python51学习