如何在已经创建的virtualenv中设置python路径?

时间:2021-10-05 23:13:42

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

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

6 个解决方案

#1


92  

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

为了使它恢复到原来的值,你可以添加。

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"

#2


48  

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-package /_virtualenv_path_extensions.pth。

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

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

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中找到该特性的特定文档。

#3


2  

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

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

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.

效果与添加/一些/库/路径到sys相同。路径,并保持本地到virtualenv的设置。

#4


1  

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.. It's a drop-in replacement for the activate script created by virtualenv 1.11.6.

您可以在这里找到补丁激活脚本。它是由virtualenv 1.11.6创建的激活脚本的替代。

Then I added something like this to my .virtualenvrc:

然后我在。virtualenvrc中添加了这样的东西:

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

#5


1  

After initializing your vertualenv(1. cd venv and 2. source bin/activate).

初始化后vertualenv(1。cd venv和2。源bin /激活)。

just set or change your python path by entering command following-

通过输入以下命令来设置或更改您的python路径。

export PYTHONPATH='/home/django/srmvenv/lib/python3.4'

出口到PYTHONPATH = ' / home / django srmvenv / lib / python3.4 '

for checking python path enter 1)

用于检查python路径输入1)

type  python then 
>> import sys
>> sys.path

Thanks you...

谢谢你……

#6


-16  

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

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

UNIX/LINUX

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

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

WINDOWS 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的变量。如果您已经有了,请检查它指向正确的目录。如果您还没有,请单击New按钮并创建它。

PYTHON CODE

Alternatively, you can also do below your code:-

或者,您也可以在您的代码下面做:-。

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

#1


92  

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

为了使它恢复到原来的值,你可以添加。

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"

#2


48  

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-package /_virtualenv_path_extensions.pth。

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

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

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中找到该特性的特定文档。

#3


2  

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

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

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.

效果与添加/一些/库/路径到sys相同。路径,并保持本地到virtualenv的设置。

#4


1  

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.. It's a drop-in replacement for the activate script created by virtualenv 1.11.6.

您可以在这里找到补丁激活脚本。它是由virtualenv 1.11.6创建的激活脚本的替代。

Then I added something like this to my .virtualenvrc:

然后我在。virtualenvrc中添加了这样的东西:

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

#5


1  

After initializing your vertualenv(1. cd venv and 2. source bin/activate).

初始化后vertualenv(1。cd venv和2。源bin /激活)。

just set or change your python path by entering command following-

通过输入以下命令来设置或更改您的python路径。

export PYTHONPATH='/home/django/srmvenv/lib/python3.4'

出口到PYTHONPATH = ' / home / django srmvenv / lib / python3.4 '

for checking python path enter 1)

用于检查python路径输入1)

type  python then 
>> import sys
>> sys.path

Thanks you...

谢谢你……

#6


-16  

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

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

UNIX/LINUX

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

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

WINDOWS 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的变量。如果您已经有了,请检查它指向正确的目录。如果您还没有,请单击New按钮并创建它。

PYTHON CODE

Alternatively, you can also do below your code:-

或者,您也可以在您的代码下面做:-。

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