Linux 如何激活virtualenv?

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

How to activate virtualenv?

pythonlinuxvirtualenv

提问by larry

I have been through search and tried various alternatives without success and spent several days on it now - driving me mad.

我已经通过搜索并尝试了各种替代方案但没有成功,现在花了几天时间 - 让我发疯。

Running on Red Hat Linux with Python 2.5.2 Began using most recent Virtualenv but could not activate it, I found somewhere suggesting needed earlier version so I have used Virtualenv 1.6.4 as that should work with Python 2.6.

在 Red Hat Linux 上运行 Python 2.5.2 开始使用最新的 Virtualenv 但无法激活它,我发现某处建议需要早期版本,所以我使用了 Virtualenv 1.6.4,因为它应该适用于 Python 2.6。

It seems to install the virtual environment ok

安装虚拟环境好像ok

[necrailk@server6 ~]$ python virtualenv-1.6.4/virtualenv.py virtual
New python executable in virtual/bin/python
Installing setuptools............done.
Installing pip...............done.

Environment looks ok

环境看起来还可以

[necrailk@server6 ~]$ cd virtual
[necrailk@server6 ~/virtual]$ dir
bin  include  lib

Trying to activate

尝试激活

[necrailk@server6 ~/virtual]$ . bin/activate
/bin/.: Permission denied.

Checked chmod

检查 chmod

[necrailk@server6 ~/virtual]$ cd bin
[necrailk@server6 bin]$ ls -l
total 3160
-rw-r--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
-rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
-rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
-rw-r--r-

Problem, so I changed it

问题,所以我改了

[necrailk@server6 bin]$ ls -l
total 3160
-rwxr--r--    1 necrailk biz12        2130 Jan 30 11:38 activate
-rw-r--r--    1 necrailk biz12        1050 Jan 30 11:38 activate.csh
-rw-r--r--    1 necrailk biz12        2869 Jan 30 11:38 activate.fish
-rw-r--r--    1 necrailk biz12        1005 Jan 30 11:38 activate_this.py
-rwxr-xr-x    1 necrailk biz

Try activateagain

再试activate一次

[necrailk@server6 ~/virtual]$ . bin/activate
/bin/.: Permission denied.

Still no joy...

还是没有喜...

回答by kojiro

The problem there is the /bin/.command. That's really weird, since . should always be a link to the directory it's in. (Honestly, unless .is a strange alias or function, I don't even see how it's possible.) It's also a little unusual that your shell doesn't have a .builtin for source.

问题在于/bin/.命令。这真的很奇怪,因为。应该始终是它所在目录的链接。(老实说,除非.是一个奇怪的别名或函数,否则我什至不知道这是怎么可能的。)你的 shell 没有.内置source.

One quick fix would be to just run the virtualenv in a different shell. (An obvious second advantage being that instead of having to deactivateyou can just exit.)

一个快速解决方法是在不同的 shell 中运行 virtualenv。(第二个明显的优势是,deactivate您不必这样做就可以exit。)

/bin/bash --rcfile bin/activate

If your shell supports it, you may also have the nonstandard sourcecommand, which shoulddo the same thing as ., but may not exist. (All said, you should try to figure out why your environment is strange or it will cause you pain again in the future.)

如果您的 shell 支持它,您可能还有非标准source命令,它应该与做同样的事情.,但可能不存在。(总而言之,你应该试着弄清楚为什么你的环境是奇怪的,否则它会在未来再次让你痛苦。)

By the way, you didn't need to chmod +xthose files. Files only need to be executable if you want to execute them directly. In this case you're trying to launch them from ., so they don't need it.

顺便说一下,您不需要chmod +x这些文件。如果您想直接执行它们,文件只需要是可执行的。在这种情况下,您尝试从 启动它们.,因此它们不需要它。

回答by Erika

I would recommend virtualenvwrapper as well. It works wonders for me and how I always have problems with activating. http://virtualenvwrapper.readthedocs.org/en/latest/

我也会推荐 virtualenvwrapper 。它为我创造了奇迹,以及我如何总是在激活时遇到问题。http://virtualenvwrapper.readthedocs.org/en/latest/

回答by topherjaynes

Here is my workflow after creating a folder and cd'ing into it:

这是我创建文件夹并cd进入后的工作流程:

$ virtualenv venv --distribute
New python executable in venv/bin/python
Installing distribute.........done.
Installing pip................done.
$ source venv/bin/activate
(venv)$ python

回答by topherjaynes

You forgot to do source bin/activatewhere source is a executable name. Struck me first few times as well, easy to think that manual is telling "execute this from root of the environment folder".

您忘记source bin/activate在 source 是可执行文件名的地方做。前几次也让我震惊,很容易认为手册是在告诉“从环境文件夹的根目录执行此操作”。

No need to make activateexecutable via chmod.

无需activate通过chmod.

回答by Jason

I had trouble getting running source /bin/activate then I realized I was using tcsh as my terminal shell instead of bash. once I switched I was able to activate venv.

我在运行 source /bin/activate 时遇到了麻烦,然后我意识到我使用 tcsh 作为终端 shell 而不是 bash。一旦我切换,我就能够激活venv。

回答by Manas

Create your own Python virtual environment called <Your Env _name >:. I have given it VE.

创建您自己的 Python 虚拟环境,名为<Your Env _name >:. 我给了它VE。

git clone https://github.com/pypa/virtualenv.git
python virtualenv.py VE

To activate your new virtual environment, run (notice it's not ./here):

要激活您的新虚拟环境,请运行(注意它不在./此处):

. VE/bin/activate

Sample output (note prompt changed):

示例输出(注意提示已更改):

(VE)c34299@a200dblr$

Once your virtual environment is set, you can remove the Virtualenvrepo.

设置虚拟环境后,您可以删除存储Virtualenv库。

回答by cquptzzq

You can do

你可以做

source ./python_env/bin/activate

or just go to the directory

或者直接进入目录

cd /python_env/bin/

and then

进而

source ./activate

Good Luck.

祝你好运。

回答by Lokendra

Probably a little late to post my answer here but still I'll post, it might benefit someone though,

在这里发布我的答案可能有点晚了,但我仍然会发布,但它可能会让某人受益,

I had faced the same problem,

我遇到了同样的问题,

The main reason being that I created the virtualenv as a "root" user But later was trying to activate it using another user.

主要原因是我以“root”用户身份创建了 virtualenv 但后来尝试使用另一个用户激活它。

chmod won't work as you're not the owner of the file, hence the alternative is to use chown (to change the ownership)

chmod 将不起作用,因为您不是文件的所有者,因此另一种方法是使用 chown(更改所有权)

For e.g. :

例如:

If you have your virtualenv created at /home/abc/ENV

如果您在 /home/abc/ENV 创建了 virtualenv

Then CD to /home/abc

然后CD到/home/abc

and run the command : chown -Rv[user-to-whom-you want-change-ownership] [folder/filename whose ownership needs to be changed]

并运行命令:chown -Rv [user-to-whom-you want-change-ownership] [需要更改所有权的文件夹/文件名]

In this example the commands would be : chown -Rv abc ENV

在这个例子中,命令是:chown -Rv abc ENV

After the ownership is successfully changed you can simply run source /ENV/bin/./activateand your should be able to activate the virtualenv correctly.

成功更改所有权后,您只需运行source /ENV/bin/./activate即可正确激活 virtualenv。

回答by Joy Mukherjee

instead of ./activate

代替 ./activate

use source activate

source activate

See this screenshot

看这个截图

回答by Reihan_amn

Cd to the environment path, go to the bin folder. At this point when you use ls command, you should see the "activate" file.

cd到环境路径,进入bin文件夹。此时,当您使用 ls 命令时,您应该会看到“激活”文件。

now type

现在输入

source activate