sys.
path
A list of strings that specifies the search path for modules. Initialized from the environment variable
PYTHONPATH
, plus an installation-dependent default.
As initialized upon program startup, the first item of this list,
path[0]
, is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0]
is the empty string, which directs Python to search modules in the current directory first. Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH
.
這段寫的是如果 sys.path 回傳的list 的第一個元是空括號。代表python 會搜索目前所在
的資料夾。然後才是接下來所有的元。然後python 會搜索在pythonpath 裡面的packages.
In nest_project virtual environment :
>>> import sys
>>> print sys.path
['', '/Users/pohsuanhuang/.local/lib/python2.7/site-packages/nupic-0.5.5.dev0-py2.7.egg','/Users/pohsuanhuang/miniconda/envs/nupic/lib/python2.7/site-packages/nupic.bindings-0.4.5.dev0-py2.7-macosx-10.6-x86_64.egg', '/Applications/Nest/ins/lib/python2.7/site-packages', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python27.zip', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/plat-darwin', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/plat-mac', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/lib-tk', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/lib-old', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/lib-dynload', '/Users/pohsuanhuang/.local/lib/python2.7/site-packages', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/site-packages', '/Users/pohsuanhuang/miniconda/envs/nest_project/lib/python2.7/site-packages/cycler-0.10.0-py2.7.egg']
>>> import os
>>> os.environ['PYTHONPATH'].split(os.pathsep)
['/Applications/Nest/ins/lib/python2.7/site-packages']
import sys
print(sys.path)
['', '/Users/pohsuanhuang/.local/lib/python2.7/site-packages/nupic-0.5.5.dev0-py2.7.egg', '/Users/pohsuanhuang/miniconda/envs/nupic/lib/python2.7/site-packages/nupic.bindings-0.4.5.dev0-py2.7-macosx-10.6-x86_64.egg', '/Applications/Nest/ins/lib/python2.7/site-packages', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python27.zip', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/plat-darwin', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/plat-mac', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/lib-tk', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/lib-old', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/lib-dynload', '/Users/pohsuanhuang/.local/lib/python2.7/site-packages', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages', '/Users/pohsuanhuang/miniconda/envs/tensorflow/lib/python2.7/site-packages/IPython/extensions', '/Users/pohsuanhuang/.ipython']