python - 是否可以在 Windows 上使用 PYTHONPATH 的多个路径?

标签 python pythonpath

我有一个干净的虚拟机,带有 XP SP3 32 位和 ActivePython 2.7.2 32 位社区版,当前目录是“C:\test”。我已将 sitecustomize.py 与“print( 'dir1' )”代码独立“C:\test\dir1”和 sitecustomize.py 与“print( 'dir2' )”代码独立“C:\test\dir2”。

如果我将PYTHONPATH设置为dir1dir2,则执行相应的sitecustomize.py:

C:\test> set PYTHONPATH=C:\test\dir1\
C:\text> python
dir1
>>>exit()
C:\test> set PYTHONPATH=C:\test\dir2\
C:\text> python
dir2

但是如果我将两个目录都添加到 pythonpath 中,则仅执行 first 目录的 sitecustomize.py :

C:\test> set PYTHONPATH=C:\test\dir1\;C:\test\dir2\
C:\text> python
dir1
>>>exit()
C:\test> set PYTHONPATH=C:\test\dir2\;C:\test\dir1\
C:\text> python
dir2

那么,PYTHONPATH 和多个 sitecustomize.py 中是否可以有多个目录,或者我仅限于一个?文档指出我可以在 PYTHONPATH 中拥有许多目录,但它没有说明 sitecustomize.py

最佳答案

是的,您可以在 PYTHONPATH 中列出多个目录。

sitecustomize 是一个在解释器启动时自动导入的模块。因此,如果您的 PYTHONPATH 上有多个 sitecustomize.py,则只会加载第一个。

该过程在documentation中描述。 :

After these path manipulations, an attempt is made to import a module named sitecustomize, which can perform arbitrary site-specific customizations. It is typically created by a system administrator in the site-packages directory. If this import fails with an ImportError exception, it is silently ignored.

关于python - 是否可以在 Windows 上使用 PYTHONPATH 的多个路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14415353/

相关文章:

python - 如何将从命令行安装的 pip 模块添加到 PyDev 环境中?

macos - 如何为root用户设置环境变量

python - 如何使用同一字典的另一个键设置字典值

python - 如何设置 Scrapy Auto_Throttle 设置

python - 在突变中传递链接数组 [graphene/python/graphql]

python - 如何从 sys.path 中永久删除值?

python - 使用 XLRD 迭代列中的行

python - 在 python 增强生成器中避免 "exception ignored"

包开发过程中的python路径

python - 如何让我的电脑知道我现在想使用 python 3?