`PYTHONPATH`真的是一个环境变量吗?

huangapple go评论86阅读模式
英文:

Is `PYTHONPATH` really an environment variable?

问题

The docs for sys.path state the following:

> A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

So my understanding here is that PYTHONPATH is an environment variable. Environment variables can be printed out in Powershell using the following command:

PS> echo $ENV:VARIABLENAME

However when I do $ENV:PYTHONPATH I get no output. If I try to access PYTHONPATH from a python terminal, I get a KeyError:

>>> import os
>>> os.environ["PYTHONPATH"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python310\lib\os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'PYTHONPATH'

However, I know PYTHONPATH is defined somewhere, because its value does appear when I use sys.path:

>>> import sys
>>> sys.path
['', 'C:\\Python310\\python310.zip', 'C:\\Python310\\DLLs', 'C:\\Python310\\lib', 'C:\\Python310', 'C:\\Users\\aa\\AppData\\Roaming\\Python\\Python310\\site-packages', 'C:\\Python310\\lib\\site-packages', 'C:\\Python310\\lib\\site-packages\\scons-4.4.0-py3.10.egg', 'C:\\Python310\\lib\\site-packages\\colorama-0.3.2-py3.10.egg', 'C:\\Python310\\lib\\site-packages\\win32', 'C:\\Python310\\lib\\site-packages\\win32\\lib', 'C:\\Python310\\lib\\site-packages\\Pythonwin']

If PYTHONPATH is truly an environment variable, why can't I access it using either Powershell or os in my Python interpreter?

英文:

The docs for sys.path state the following:

> A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

So my understanding here is that PYTHONPATH is an environment variable. Environment variables can be printed out in Powershell using the following command:

PS> echo $ENV:VARIABLENAME

However when I do $ENV:PYTHONPATH I get no output. If I try to access PYTHONPATH from a python terminal, I get a KeyError:

>>> import os
>>> os.environ["PYTHONPATH"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python310\lib\os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'PYTHONPATH'

However, I know PYTHONPATH is defined somewhere, because its value does appear when I use sys.path:

>>> import sys
>>> sys.path
['', 'C:\\Python310\\python310.zip', 'C:\\Python310\\DLLs', 'C:\\Python310\\lib', 'C:\\Python310', 'C:\\Users\\aa\\AppData\\Roaming\\Python\\Python310\\site-packages', 'C:\\Python310\\lib\\site-packages', 'C:\\Python310\\lib\\site-packages\\scons-4.4.0-py3.10.egg', 'C:\\Python310\\lib\\site-packages\\colorama-0.3.2-py3.10.egg', 'C:\\Python310\\lib\\site-packages\\win32', 'C:\\Python310\\lib\\site-packages\\win32\\lib', 'C:\\Python310\\lib\\site-packages\\Pythonwin']

If PYTHONPATH is truly an environment variable, why can't I access it using either Powershell or os in my Python interpreter?

答案1

得分: 3

The variable PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. This variable is not set by default and not needed for Python to work because it already knows where to find its standard libraries (sys.path).

But if for any reason you need some custom Python libraries that you do not want to install, you can export PYTHONPATH=/path/to/my/modules/ to add /path/to/my/modules/ so Python will then know where to find those custom modules.

And this time, if you print again sys.path, it will display you the newly added modules.

英文:

The variable PYTHONPATH is an environment variable which you can set to add additional directories where python will look for modules and packages. This variable is not set by default and not needed for Python to work because it it already knows where to find its standard libraries (sys.path).

But if for any reason you need some custom Python libraries that you do not want to install, you can export PYTHONPATH=/path/to/my/modules/ to add /path/to/my/modules/ son Python will then know where to find those custom modules.

And this time, if you print again sys.path, it will display you the newly added modules.

huangapple
  • 本文由 发表于 2023年5月6日 18:09:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定