sys.path
sys.path是python的搜索模块的路径集,是一个list
可以通过ipython终端查看
In [7]: import sys
In [8]: from sys import path
In [9]: path
或者
In [10]: sys.path
结果如下:
[
...(略)
'd:\\programs\\python\\python 3.7.2\\lib',
'',(执行文件当前路径?-存疑)
'd:\\programs\\python\\python 3.7.2\\lib\\site-packages',
...(略)
]
可以在python 环境下使用sys.path.append(path)添加相关的路径,但是
退出python环境后自己添加的路径就会自动消失!
C:\Users\Administrator>ipython
Python 3.7.2
...(略)
In [1]: import sys
In [2]: sys.path[-2:]
Out[2]:
['d:\\programs\\python\\python 3.7.2\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\Administrator\\.ipython']
In [3]: sys.path.append("f:\\")
In [4]: sys.path[-2:]
Out[4]: ['C:\\Users\\Administrator\\.ipython', 'f:\\']
In [5]: exit
--------------------
退出重新执行ipython,'f:\\'自动消失
--------------------
C:\Users\Administrator>ipython
Python 3.7.2Python 3.7.2
...(略)
In [1]: import sys
In [2]: sys.path[-2:]
Out[2]:
['d:\\programs\\python\\python 3.7.2\\lib\\site-packages\\IPython\\extensions',
'C:\\Users\\Administrator\\.ipython']
为解决这个问题,可以有以下方法:
一、使用 site_packages 目录
将自己做的py文件放到 site_packages 目录下
上面显示的sys.path中,已经包含了这个搜索目录,当然,也可以通过如下方式单独查看