问题:如何在virtualenv中向PYTHONPATH添加路径

我正在尝试向PYTHONPATH环境变量添加路径,该路径仅在特定的virtualenv环境中可见。

SET PYTHONPATH=...在virtualenv命令提示符下进行了尝试,但这为整个环境设置了变量。

我该如何实现?

I am trying to add a path to the PYTHONPATH environment variable, that would be only visible from a particular virtualenv environment.

I tried SET PYTHONPATH=... under a virtualenv command prompt, but that sets the variable for the whole environment.

How do I achieve that?


回答 0

通常,您可以通过来避免对PYTHONPATH做任何事情。只需在您的virtualenv的site-packages文件夹中放入一个扩展名为.pth的文件(任何基本名称均有效),例如lib\python2.7\site-packages,将包含软件包的目录的绝对路径作为其唯一内容。

You can usually avoid having to do anything with PYTHONPATH by . Just put a file with a .pth extension (any basename works) in your virtualenv’s site-packages folder, e.g. lib\python2.7\site-packages, with the absolute path to the directory containing your package as its only contents.


回答 1

如果使用virtualenv,则可能还应该使用virtualenvwrapper,在这种情况下,可以使用add2virtualenv命令将路径添加到当前virtualenv的Python路径:

add2virtualenv directory1 directory2 …

If you’re using virtualenv, you should probably also be using virtualenvwrapper, in which case you can use the add2virtualenv command to add paths to the Python path for the current virtualenv:

add2virtualenv directory1 directory2 …


回答 2

您也可以尝试将符号链接放入您的virtualenv之一。

例如。1)激活您的virtualenv 2)运行python 3)导入sys并检查sys.path 4)您将在那里找到python搜索路径。选择其中一个(例如站点软件包)5)转到其中,并创建指向您软件包的符号链接,例如:ln -s您要导入的软件包名称路径

这样,即使不激活virtualenv,您也应该能够导入它。只需尝试:进入您的virtualenv-folder / bin / python路径并导入您的包。

You can also try to put symlink to one of your virtualenv.

eg. 1) activate your virtualenv 2) run python 3) import sys and check sys.path 4) you will find python search path there. Choose one of those (eg. site-packages) 5) go there and create symlink to your package like: ln -s path-to-your-package name-with-which-you’ll-be-importing

That way you should be able to import it even without activating your virtualenv. Simply try: path-to-your-virtualenv-folder/bin/python and import your package.


回答 3

如果您使用virtualenvwrapper,

$ cd to the parent folder
$ add2virtualenv  folder_to_add

控制台将显示

Warning: Converting "folder_to_add" to "/absoutle/path/to/folder_to_add"

就是这样,您应该很好

If you are using virtualenvwrapper,

$ cd to the parent folder
$ add2virtualenv  folder_to_add

console will display

Warning: Converting "folder_to_add" to "/absoutle/path/to/folder_to_add"

That’s it, and you should be good to go


回答 4

import sys
import os

print(str(sys.path))

dir_path = os.path.dirname(os.path.realpath(__file__))
print("current working dir: %s" % dir_path)

sys.path.insert(0, dir_path)

我强烈建议您使用virtualenv和virtualenvwrapper以避免路径混乱。

import sys
import os

print(str(sys.path))

dir_path = os.path.dirname(os.path.realpath(__file__))
print("current working dir: %s" % dir_path)

sys.path.insert(0, dir_path)

I strongly suggest you use virtualenv and virtualenvwrapper to avoid cluttering the path.


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。