问题:如何在已经创建的virtualenv中设置pythonpath?

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

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


回答 0

编辑#2

正确的答案是@arogachev的答案。


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

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

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

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

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

export OLD_PYTHONPATH="$PYTHONPATH"

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

export PYTHONPATH="$OLD_PYTHONPATH"

EDIT #2

The right answer is @arogachev’s one.


If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv’s bin/activate file:

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

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

EDIT: (to answer @RamRachum’s comment)

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

export OLD_PYTHONPATH="$PYTHONPATH"

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

export PYTHONPATH="$OLD_PYTHONPATH"

回答 1

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

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

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

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

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

有关此功能的特定文档,请访问 http://virtualenvwrapper.readthedocs.org/en/latest/command_ref.html?highlight=add2virtualenv

The comment by @s29 should be an answer:

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

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

Documentation on virtualenvwrapper can be found at 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


回答 2

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

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

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

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

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/path to sys.path, and remain local to the virtualenv setup.


回答 3

  1. 初始化您的virtualenv
cd venv

source bin/activate
  1. 只需通过输入以下命令来设置或更改python路径:
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
  1. 用于检查python路径,请在python中输入:
   python

      \>\> import sys

      \>\> sys.path
  1. Initialize your virtualenv
cd venv

source bin/activate
  1. Just set or change your python path by entering command following:
export PYTHONPATH='/home/django/srmvenv/lib/python3.4'
  1. for checking python path enter in python:
   python

      \>\> import sys

      \>\> sys.path


回答 4

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

您可以在activate此处找到修补的脚本。。它是由virtualenv 1.11.6创建的激活脚本的直接替代。

然后我向我添加了类似的内容.virtualenvrc

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

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

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

Then I added something like this to my .virtualenvrc:

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

回答 5

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

UNIX / Linux

将“ export PYTHONPATH = / usr / local / lib / python2.0”添加到〜/ .bashrc文件,并通过键入“ source〜/ .bashrc”或“。〜/ .bashrc”将其导出。

Windows XP

1)转到控制面板2)双击系统3)转到高级选项卡4)单击环境变量

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

密码

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

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

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

UNIX/LINUX

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

WINDOWS XP

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

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.

PYTHON CODE

Alternatively, you can also do below your code:-

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

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