如何激活virtualenv?

问题:如何激活virtualenv?

我一直在搜索,尝试了各种替代方法都没有成功,现在花了几天时间-令我发疯。

在具有Python 2.5.2的Red Hat Linux上运行开始使用最新的Virtualenv,但无法激活它,我发现某个地方提示需要较早的版本,因此我使用Virtualenv 1.6.4,因为它应可与Python 2.6一起使用。

看来安装虚拟环境还可以

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

环境看起来还可以

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

试图激活

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

已检查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-

问题,所以我改变了

[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

再试activate一次

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

仍然没有喜悦…

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.

It seems to install the virtual environment 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

[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 activate again

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

Still no joy…


回答 0

这是创建文件夹并将cd其放入后的工作流程:

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

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

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

回答 1

您忘了source bin/activate在source是可执行文件名称的地方做。同样也打了我几次,容易想到手册说的是“从环境文件夹的根目录执行此操作”。

无需activate通过执行chmod

You forgot to do source bin/activate where 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”.

No need to make activate executable via chmod.


回答 2

你可以做

source ./python_env/bin/activate

或只是转到目录

cd /python_env/bin/

然后

source ./activate

祝好运。

You can do

source ./python_env/bin/activate

or just go to the directory

cd /python_env/bin/

and then

source ./activate

Good Luck.


回答 3

将CD复制到环境路径,然后转到bin文件夹。此时,当您使用ls命令时,应该会看到“激活”文件。

现在输入

source activate

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

now type

source activate

回答 4

转到项目目录。在我的情况下microblog是flask项目目录,在microblog目录下应该有appvenv文件夹。然后运行以下命令,这是在Ubuntu中为我工作的命令。

source venv/bin/activate

Go to the project directory. In my case microblog is the flask project directory and under microblog directory there should be app and venv folders. then run the below command, This is one worked for me in Ubuntu.

source venv/bin/activate


回答 5

问题出在/bin/.命令。从那以来,那真的很奇怪。应该始终是它所在目录的链接。(老实说,除非.是一个奇怪的别名或函数,否则我什至不知道这是不可能的。)shell没有. 内置的forsource也是有点不寻常

一种快速的解决方法是仅在另一个Shell中运行virtualenv。(一个明显的第二个优势是,deactivate您不必仅仅拥有即可exit。)

/bin/bash --rcfile bin/activate

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

顺便说一句,您不需要chmod +x这些文件。仅当您要直接执行文件时,才需要文件是可执行文件。在这种情况下,您尝试从中启动它们.,因此他们不需要它。

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.

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

/bin/bash --rcfile bin/activate

If your shell supports it, you may also have the nonstandard source command, which should do 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.)

By the way, you didn’t need to chmod +x those 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.


回答 6

$ mkdir <YOURPROJECT> 创建一个新项目

$ cd <YOURPROJECT> 将目录更改为该项目

$ virtualenv <NEWVIRTUALENV> 创建新的virtualenv

$ source <NEWVIRTUALENV>/bin/activate 激活新的virtualenv

$ mkdir <YOURPROJECT> Create a new project

$ cd <YOURPROJECT> Change directory to that project

$ virtualenv <NEWVIRTUALENV> Creating new virtualenv

$ source <NEWVIRTUALENV>/bin/activate Activating that new virtualenv


回答 7

代替 ./activate

source activate

instead of ./activate

use source activate


回答 8

对于Windows,您可以执行以下操作:

创建虚拟环境的方式为:virtualenv envName –python = python.exe(如果未创建环境变量)

激活虚拟环境:> \ path \ to \ envName \ Scripts \ activate

要停用虚拟环境:> \ path \ to \ env \ Scripts \ deactivate

可以在新的python版本上正常工作。

For Windows You can perform as:

TO create the virtual env as: virtualenv envName –python=python.exe (if not create environment variable)

To activate the virtual env : > \path\to\envName\Scripts\activate

To deactivate the virtual env : > \path\to\env\Scripts\deactivate

It fine works on the new python version .


回答 9

source virtualen_name/bin/activate

source virtualen_name/bin/activate


回答 10

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

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/


回答 11

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

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

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

. VE/bin/activate

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

(VE)c34299@a200dblr$

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

Create your own Python virtual environment called <Your Env _name >:. I have given it 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 Virtualenv repo.


回答 12

在Mac上,将shell更改为BASH(请注意,虚拟环境仅在bash shell中有效)

[user@host tools]$. venv/bin/activate 

.: Command not found.

[user@host tools]$source venv/bin/activate

Badly placed ()'s.

[user@host tools]$bash

bash-3.2$ source venv/bin/activate

(venv) bash-3.2$ 

宾果游戏,它奏效了。看到提示更改。

在Ubuntu上:

user@local_host:~/tools$ source toolsenv/bin/activate

(toolsenv) user@local_host~/tools$ 

注意:提示更改

On Mac, change shell to BASH (keep note that virtual env works only in bash shell )

[user@host tools]$. venv/bin/activate 

.: Command not found.

[user@host tools]$source venv/bin/activate

Badly placed ()'s.

[user@host tools]$bash

bash-3.2$ source venv/bin/activate

(venv) bash-3.2$ 

Bingo , it worked. See prompt changed.

On Ubuntu:

user@local_host:~/tools$ source toolsenv/bin/activate

(toolsenv) user@local_host~/tools$ 

Note : prompt changed


回答 13

我在运行源代码/ bin / activate时遇到了麻烦,然后我意识到我将tcsh用作终端shell而不是bash。切换后,我便可以激活venv。

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.


回答 14

Windows 10

在Windows中,将创建以下目录:

在Windows 10中激活虚拟环境。

down\scripts\activate

\ scripts目录包含激活文件。

Linux Ubuntu

在Ubuntu中,将创建以下目录:

在Linux Ubuntu中激活虚拟环境。

source ./bin/activate

/ bin目录包含激活文件。


虚拟环境从Windows复制到Linux Ubuntu反之亦然

如果将虚拟环境文件夹从Windows复制到Linux Ubuntu,则根据目录:

source ./down/Scripts/activate

Windows 10

In Windows these directories are created :

To activate Virtual Environment in Windows 10.

down\scripts\activate

\scripts directory contain activate file.

Linux Ubuntu

In Ubuntu these directories are created :

To activate Virtual Environment in Linux Ubuntu.

source ./bin/activate

/bin directory contain activate file.


Virtual Environment copied from Windows to Linux Ubuntu vice versa

If Virtual environment folder copied from Windows to Linux Ubuntu then according to directories:

source ./down/Scripts/activate

回答 15

运行此代码,如果您在Windows计算机上,它将被激活
source venv/Scripts/activate

run this code it will get activated if you on a windows machine
source venv/Scripts/activate


回答 16

在这里发布我的答案可能有点晚了,但我仍然会发布,尽管这样做可能会使某人受益,

我曾经遇到过同样的问题,

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

chmod不能工作,因为您不是文件的所有者,因此替代方法是使用chown(更改所有权)

例如:

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

然后CD到/ home / abc

并运行以下命令:chown -Rv [您想要更改所有权的用户] [需要更改所有权的文件夹/文件名]

在此示例中,命令为:chown -Rv abc ENV

成功更改所有权后,您只需运行源/ENV/bin/./activate,您就应该能够正确激活virtualenv。

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.

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

For e.g. :

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

Then CD to /home/abc

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

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

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


回答 17

1-打开powershell并导航到您的应用程序文件夹2-输入您的virtualenv文件夹,例如:cd。\ venv \ Scripts \ 3-通过键入。\ activate激活virtualenv

1- open powershell and navigate to your application folder 2- enter your virtualenv folder ex : cd .\venv\Scripts\ 3- active virtualenv by type .\activate