centos7设置python路径

直接在命令行运行.py 文件:

[clouder@ana53 common]$ python3 driver.py
Traceback (most recent call last):
  File "driver.py", line 3, in <module>
    from scripts.common import read_conf
ModuleNotFoundError: No module named 'scripts'

由于driver.py 引用了自己写的一个模块,没有加到PYTHONPATH,直接在pycharm运行是没问题,因为pycharm会默认把当前project加到sys.path.但是在命令行则不会报找不到自定义模块的错误

[clouder@ana53 common]$ python3
Python 3.6.3 (default, Dec  5 2017, 14:08:32) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.path)
['', '/opt/python36/lib/python36.zip', '/opt/python36/lib/python3.6', '/opt/python36/lib/python3.6/lib-dynload', '/opt/python36/lib/python3.6/site-packages']
>>> sys.path.append('home/clouder/workspace/pycharm/xiaohui/scripts/common')
>>> print(sys.path)
['', '/opt/python36/lib/python36.zip', '/opt/python36/lib/python3.6', '/opt/python36/lib/python3.6/lib-dynload', '/opt/python36/lib/python3.6/site-packages', 'home/clouder/workspace/pycharm/xiaohui/scripts/common']

想要在命令行也可以执行,手动把project项目路径加到PATHONPATH,默认没有这个环境变量的:

修改自己~目录下的.bashrc,增加一行,并source一下生效.只对当前用户生效.也可以修改/etc/profile文件,对所有用户生效.方法一致
[clouder@ana53 lib]$ echo 'export PYTHONPATH=/home/clouder/workspace/pycharm/xiaohui' >> ~/.bashrc
[clouder@ana53 lib]$ source ~/.bashrc
posted @ 2019-01-26 15:49  2019勇往直前  阅读(3393)  评论(0编辑  收藏  举报