Linux 你如何在已经创建的 virtualenv 中设置你的 pythonpath?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4757178/
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-04 00:10:58  来源:igfitidea点击:

How do you set your pythonpath in an already-created virtualenv?

pythonlinuxunixvirtualenv

提问by TIMEX

What file do I edit, and how? I created a virtual environment.

我要编辑什么文件,如何编辑?我创建了一个虚拟环境。

采纳答案by mdeous

EDIT #2

编辑#2

The right answer is @arogachev's one.

正确答案是@arogachev 的答案。



If you want to change the PYTHONPATHused in a virtualenv, you can add the following line to your virtualenv's bin/activatefile:

如果要更改PYTHONPATHvirtualenv 中的used,可以将以下行添加到 virtualenv 的bin/activate文件中:

export PYTHONPATH="/the/path/you/want"

This way, the new PYTHONPATHwill be set each time you use this virtualenv.

这样,PYTHONPATH每次使用这个 virtualenv 时都会设置新的。

EDIT:(to answer @RamRachum's comment)

编辑:(回答@RamRachum 的评论)

To have it restored to its original value on deactivate, you could add

要将其恢复到其原始值deactivate,您可以添加

export OLD_PYTHONPATH="$PYTHONPATH"

before the previously mentioned line, and add the following line to your bin/postdeactivatescript.

在前面提到的行之前,并将以下行添加到您的bin/postdeactivate脚本中。

export PYTHONPATH="$OLD_PYTHONPATH"

回答by Ravikiran

It's already answered here -> Is my virtual environment (python) causing my PYTHONPATH to break?

它已经在这里回答 ->我的虚拟环境(python)是否导致我的 PYTHONPATH 中断?

UNIX/LINUX

UNIX/Linux

Add "export PYTHONPATH=/usr/local/lib/python2.0" this to ~/.bashrc file and source it by typing "source ~/.bashrc" OR ". ~/.bashrc".

将“export PYTHONPATH=/usr/local/lib/python2.0”添加到 ~/.bashrc 文件并通过键入“source ~/.bashrc”或“. ~/.bashrc”来获取它。

WINDOWS XP

视窗XP

1) Go to the Control panel 2) Double click System 3) Go to the Advanced tab 4) Click on Environment Variables

1) 进入控制面板 2) 双击系统 3) 进入高级选项卡 4) 点击环境变量

In the System Variables window, check if you have a variable named PYTHONPATH. If you have one already, check that it points to the right directories. If you don't have one already, click the New button and create it.

在“系统变量”窗口中,检查您是否有名为 PYTHONPATH 的变量。如果您已经有一个,请检查它是否指向正确的目录。如果您还没有,请单击“新建”按钮并创建它。

PYTHON CODE

蟒蛇代码

Alternatively, you can also do below your code:-

或者,您也可以在代码下方执行以下操作:-

import sys
sys.path.append("/home/me/mypy") 

回答by tjb

The comment by @s29 should be an answer:

@s29 的评论应该是一个答案:

One way to add a directory to the virtual environment is to install virtualenvwrapper (which is useful for many things) and then do

将目录添加到虚拟环境的一种方法是安装 virtualenvwrapper(这对许多事情很有用)然后执行

mkvirtualenv myenv
workon myenv
add2virtualenv . #for current directory
add2virtualenv ~/my/path

If you want to remove these path edit the file myenvhomedir/lib/python2.7/site-packages/_virtualenv_path_extensions.pth

如果要删除这些路径,请编辑文件 myenvhomedir/lib/python2.7/site-packages/_virtualenv_path_extensions.pth

Documentation on virtualenvwrapper can be found at http://virtualenvwrapper.readthedocs.org/en/latest/

关于 virtualenvwrapper 的文档可以在http://virtualenvwrapper.readthedocs.org/en/latest/找到

Specific documentation on this feature can be found at http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html?highlight=add2virtualenv

有关此功能的特定文档可以在http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html?highlight=add2virtualenv 中找到

回答by André Laszlo

I modified my activate script to source the file .virtualenvrc, if it exists in the current directory, and to save/restore PYTHONPATHon activate/deactivate.

我修改了我的激活脚本以获取文件.virtualenvrc(如果它存在于当前目录中),并PYTHONPATH在激活/停用时保存/恢复。

You can find the patched activatescript here.. It's a drop-in replacement for the activate script created by virtualenv 1.11.6.

您可以在activate此处找到修补脚本。. 它是 virtualenv 1.11.6 创建的 activate 脚本的替代品。

Then I added something like this to my .virtualenvrc:

然后我添加了这样的东西到我的.virtualenvrc

export PYTHONPATH="${PYTHONPATH:+$PYTHONPATH:}/some/library/path"

回答by Siyaram Malav

  1. Initialize your virtualenv
  1. 初始化你的 virtualenv
cd venv

source bin/activate
  1. Just set or change your python path by entering command following:
  1. 只需输入以下命令来设置或更改您的python路径:
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
  1. for checking python path enter in python:
  1. 用于检查python路径在python中输入:
   python

      \>\> import sys

      \>\> sys.path

回答by Arjen P. De Vries

You can create a .pthfile that contains the directory to search for, and place it in the site-packagesdirectory. E.g.:

您可以创建一个.pth包含要搜索的目录的文件,并将其放置在该site-packages目录中。例如:

cd $(python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())")
echo /some/library/path > some-library.pth

The effect is the same as adding /some/library/pathto sys.path, and remain local to the virtualenvsetup.

效果与添加/some/library/path到相同sys.path,并保持本地virtualenv设置。