简体   繁体   English

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

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

What file do I edit, and how?我要编辑什么文件,如何编辑? I created a virtual environment.我创建了一个虚拟环境。

The most elegant solution to this problem is here .这个问题最优雅的解决方案是这里

Original answer remains, but this is a messy solution:原始答案仍然存在,但这是一个凌乱的解决方案:


If you want to change the PYTHONPATH used in a virtualenv, you can add the following line to your virtualenv's bin/activate file:如果要更改 virtualenv 中使用的PYTHONPATH ,可以将以下行添加到 virtualenv 的bin/activate文件中:

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

This way, the new PYTHONPATH will be set each time you use this virtualenv.这样,每次使用这个 virtualenv 时都会设置新的PYTHONPATH

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/postdeactivate script.在前面提到的行之前,并将以下行添加到您的bin/postdeactivate脚本中。

export PYTHONPATH="$OLD_PYTHONPATH"

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 中找到

You can create a .pth file that contains the directory to search for, and place it in the {venv-root}/lib/{python-version}/site-packages directory.您可以创建一个包含要搜索的目录的.pth文件,并将其放置在{venv-root}/lib/{python-version}/site-packages目录中。 Eg:例如:

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.效果与将/some/library/pathsys.path ,并保持在virtualenv设置的本地。

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

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

      \>\> import sys

      \>\> sys.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.我修改了我的激活脚本以获取文件.virtualenvrc ,如果它存在于当前目录中,并在激活/停用时保存/恢复PYTHONPATH

You can find the patched activate script here.您可以在此处找到修补的activate脚本。 . . It's a drop-in replacement for the activate script created by virtualenv 1.11.6.它是 virtualenv 1.11.6 创建的 activate 脚本的替代品。

Then I added something like this to my .virtualenvrc :然后我在我的.virtualenvrc添加了这样的东西:

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

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.在“系统变量”窗口中,检查您是否有名为 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") 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM