pythonpath是什么_在PYTHONPATH中到底应该设置什么?

下面是我学到的:Python path是一个目录,添加到Python导入搜索路径“sys.path”中,该路径由current dir组成。CWD、PYTHONPATH、标准和共享库以及客户库。例如:% python3 -c "import sys;print(sys.path)"

['',

'/home/username/Documents/DjangoTutorial/mySite',

'/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload',

'/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

其中第一个路径“”表示当前目录,第二个路径是via%export PYTHONPATH=/home/username/Documents/DjangoTutorial/mySite

它可以添加到~/.bashrc以使其永久,其余的是Python标准和dynamic shared library加上第三方库,如django。

如前所述,不要乱动PYTHONHOME,即使将其设置为“”或“无”,也会导致python3外壳停止工作:% export PYTHONHOME=''

% python3

Fatal Python error: Py_Initialize: Unable to get the locale encoding

ModuleNotFoundError: No module named 'encodings'

Current thread 0x00007f18a44ff740 (most recent call first):

Aborted (core dumped)

注意,如果启动Python脚本,CWD将是脚本的目录。

例如:username@bud:~/Documents/DjangoTutorial% python3 mySite/manage.py runserver

==== Printing sys.path ====

/home/username/Documents/DjangoTutorial/mySite # CWD is where manage.py resides

/usr/lib/python3.6

/usr/lib/python3.6/lib-dynload

/usr/local/lib/python3.6/dist-packages

/usr/lib/python3/dist-packages

也可以在运行时将路径附加到sys.path:

假设您在~/Documents/Python目录中有一个文件Fibonacci.py:username@bud:~/Documents/DjangoTutorial% python3

>>> sys.path.append("/home/username/Documents")

>>> print(sys.path)

['', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload',

'/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages',

'/home/username/Documents']

>>> from Python import Fibonacci as fibo

或通过% PYTHONPATH=/home/username/Documents:$PYTHONPATH

% python3

>>> print(sys.path)

['',

'/home/username/Documents', '/home/username/Documents/DjangoTutorial/mySite',

'/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload',

'/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']

>>> from Python import Fibonacci as fibo

你可能感兴趣的:(pythonpath是什么)