python永久添加环境变量
背景
我们自动化代码在全新机器上经常出现导包失败的问题。
临时解决
这个方案可能大家都知道添加sys.path.append('你的项目路径')
,但是你要是重新打开一个终端后就会发现刚刚添加的路径不在sys.path
中了
永久添加
如何添加pth
- 首先执行
sys.path
查看导包路径:
>>> sys.path
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/tarzan/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/usr/local/lib/python3.7/dist-packages/dogtail-0.9.9-py3.7.egg', '/usr/local/lib/python3.7/dist-packages/PyAutoGUI-0.9.53-py3.7.egg', '/usr/lib/python3/dist-packages']
- 我们在上述路径中添加我们想要添加的路径
我们以/usr/lib/python3/dist-packages
为例:我想添加/home/tarzan
进去
dist-packages|⇒ cd /usr/lib/python3/dist-packages
dist-packages|⇒ sudo vim tarzan.pth
请输入密码:
验证成功
dist-packages|⇒
- 我们再次执行
sys.path
就可以看到刚刚添加的/home/tarzan
>>> import sys
>>> sys.path
['', '/usr/lib/python37.zip', '/usr/lib/python3.7', '/usr/lib/python3.7/lib-dynload', '/home/tarzan/.local/lib/python3.7/site-packages', '/usr/local/lib/python3.7/dist-packages', '/home/tarzan/Desktop/desktop-pro/pyat_activator', '/usr/local/lib/python3.7/dist-packages/dogtail-0.9.9-py3.7.egg', '/usr/local/lib/python3.7/dist-packages/PyAutoGUI-0.9.53-py3.7.egg', '/usr/lib/python3/dist-packages', '/home/tarzan']
>>>