如何在virtualenv中添加PYTHONPATH的路径

时间:2020-12-26 23:11:29

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

我正在尝试添加PYTHONPATH环境变量的路径,该路径只能从特定的virtualenv环境中看到。

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

我在virtualenv命令提示符下尝试了SET PYTHONPATH = ...但是它为整个环境设置了变量。

How do I achieve that?

如何做到这一点?

4 个解决方案

#1


127  

You can usually avoid having to do anything with PYTHONPATH by using .pth files. 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.

您通常可以使用.pth文件避免对PYTHONPATH执行任何操作。只需在你的virtualenv的site-packages文件夹中放一个扩展名为.pth的文件(任何basename都可以),例如: lib \ python2.7 \ site-packages,包含包的唯一内容的目录的绝对路径。

#2


83  

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:

如果您正在使用virtualenv,您可能也应该使用virtualenvwrapper,在这种情况下,您可以使用add2virtualenv命令为当前virtualenv的Python路径添加路径:

add2virtualenv directory1 directory2 …

add2virtualenv directory1 directory2 ...

#3


4  

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

您也可以尝试将符号链接添加到您的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

例如。 1)激活你的virtualenv 2)运行python 3)import sys并检查sys.path 4)你会在那里找到python搜索路径。选择其中一个(例如站点包)5)去那里并为你的包创建符号链接,如:ln -s path-to-your-package name-with-which-you-be-imports

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.

这样你甚至可以在不激活virtualenv的情况下导入它。只需尝试:path-to-your-virtualenv-folder / bin / python并导入您的包。

#4


0  

In Python 3.6.4

在Python 3.6.4中

import sys
import os

print(str(sys.path))

dir_path = os.path.dirname(os.path.realpath(__file__))
print(f"current working dir: {dir_path}")

sys.path.insert(0, dir_path)

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

我强烈建议你使用virtualenv和virtualenvwrapper来避免混乱的路径

#1


127  

You can usually avoid having to do anything with PYTHONPATH by using .pth files. 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.

您通常可以使用.pth文件避免对PYTHONPATH执行任何操作。只需在你的virtualenv的site-packages文件夹中放一个扩展名为.pth的文件(任何basename都可以),例如: lib \ python2.7 \ site-packages,包含包的唯一内容的目录的绝对路径。

#2


83  

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:

如果您正在使用virtualenv,您可能也应该使用virtualenvwrapper,在这种情况下,您可以使用add2virtualenv命令为当前virtualenv的Python路径添加路径:

add2virtualenv directory1 directory2 …

add2virtualenv directory1 directory2 ...

#3


4  

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

您也可以尝试将符号链接添加到您的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

例如。 1)激活你的virtualenv 2)运行python 3)import sys并检查sys.path 4)你会在那里找到python搜索路径。选择其中一个(例如站点包)5)去那里并为你的包创建符号链接,如:ln -s path-to-your-package name-with-which-you-be-imports

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.

这样你甚至可以在不激活virtualenv的情况下导入它。只需尝试:path-to-your-virtualenv-folder / bin / python并导入您的包。

#4


0  

In Python 3.6.4

在Python 3.6.4中

import sys
import os

print(str(sys.path))

dir_path = os.path.dirname(os.path.realpath(__file__))
print(f"current working dir: {dir_path}")

sys.path.insert(0, dir_path)

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

我强烈建议你使用virtualenv和virtualenvwrapper来避免混乱的路径