在导入语句之前设置 pythonpath

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15109548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:21:14  来源:igfitidea点击:

set pythonpath before import statements

pythonpathpythonpath

提问by DKG

My code is:

我的代码是:

import scriptlib.abc
import scriptlib.xyz

def foo():
  ... some operations

but the scriptlib is in some other directory, so I will have to include that directory in environment variable "PYTHONPATH".

但是脚本库位于其他目录中,因此我必须将该目录包含在环境变量“PYTHONPATH”中。

Is there anyway in which I can first add the scriptlib directory in environment variable "PYTHONPATH" before import statement getting executed like :

无论如何,在执行导入语句之前,我可以先在环境变量“PYTHONPATH”中添加 scriptlib 目录,例如:

import sys
sys.path.append('/mypath/scriptlib')
import scriptlib.abc
import scriptlib.xyz

def foo():
  ... some operations

If so, is the value only for that command prompt or is it global ?

如果是这样,该值仅适用于该命令提示符还是全局的?

Thanks in advance

提前致谢

采纳答案by Joe

This will add a path to your Python process / instance (i.e. the running executable). The path will not be modified for any other Python processes. Another running Python program will not have its path modified, and if you exit your program and run again the path will not include what you added before. What are you are doing is generally correct.

这将为您的 Python 进程/实例(即正在运行的可执行文件)添加一个路径。不会为任何其他 Python 进程修改路径。另一个正在运行的 Python 程序不会修改其路径,如果您退出程序并再次运行,路径将不包含您之前添加的内容。你在做什么通常是正确的。

set.py:

设置.py:

import sys
sys.path.append("/tmp/TEST")

loop.py

循环.py

import sys
import time
while True:
  print sys.path
  time.sleep(1)

run: python loop.py &

跑: python loop.py &

This will run loop.py, connected to your STDOUT, and it will continue to run in the background. You can then run python set.py. Each has a different set of environment variables. Observe that the output from loop.pydoes not change because set.pydoes not change loop.py's environment.

这将运行 loop.py,连接到您的 STDOUT,并将继续在后台运行。然后您可以运行python set.py. 每个都有一组不同的环境变量。观察到 的输出loop.py没有改变,因为set.py没有改变loop.py的环境。

A note on importing

进口注意事项

Python imports are dynamic, like the rest of the language. There is no static linking going on. The import is an executable line, just like sys.path.append....

Python 导入是动态的,就像语言的其余部分一样。没有进行静态链接。导入是一个可执行行,就像sys.path.append....

回答by pradyunsg

As also noted in the docs here.
Go to Python X.X/Liband add these lines to the site.pythere,

正如此处的文档中所述。
转到Python X.X/Lib并将这些行添加到site.py那里,

import sys
sys.path.append("yourpathstring")

This changes your sys.pathso that on every load, it will have that value in it..

这会更改您的内容,sys.path以便在每次加载时,它都会具有该值。

As stated hereabout site.py,

如前所述这里site.py

This module is automatically imported during initialization.Importing this module will append site-specific paths to the module search path and add a few builtins.

该模块在初始化期间自动导入。导入此模块会将特定于站点的路径附加到模块搜索路径并添加一些内置函数。

For other possible methods of adding some path to sys.pathsee these docs

对于添加一些路径以sys.path查看这些文档的其他可能方法