How to activate virtualenv in Linux?

PythonPython 3.xLinuxVirtualenvPython Venv

Python Problem Overview


I have been searching 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...

Python Solutions


Solution 1 - 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

Solution 2 - Python

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.

Solution 3 - Python

You can do

source ./python_env/bin/activate

or just go to the directory

cd /python_env/bin/

and then

source ./activate

Good Luck.

Solution 4 - Python

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

enter image description here

Solution 5 - Python

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

Solution 6 - Python

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.

Solution 7 - Python

$ 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

Solution 8 - Python

instead of ./activate

use source activate

See this screenshot

Solution 9 - Python

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

enter image description here

run this code it will get activated if you on a linux/mac machine
. venv/bin/activate

enter image description here

Solution 10 - 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 .

Solution 11 - Python

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/

Solution 12 - Python

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.

Solution 13 - Python

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

Solution 14 - Python

Windows 10

In Windows these directories are created :

Windows 10 Virtual Environment directories

To activate Virtual Environment in Windows 10.

down\scripts\activate

\scripts directory contain activate file.

Linux Ubuntu

In Ubuntu these directories are created :

Linux Ubuntu Virtual Environment directories

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

Solution 15 - Python

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.

Solution 16 - Python

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.

Solution 17 - Python

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlarryView Question on Stackoverflow
Solution 1 - PythontopherjaynesView Answer on Stackoverflow
Solution 2 - Pythonuser3292534View Answer on Stackoverflow
Solution 3 - PythoncquptzzqView Answer on Stackoverflow
Solution 4 - PythonGNKView Answer on Stackoverflow
Solution 5 - PythonReihan_amnView Answer on Stackoverflow
Solution 6 - PythonkojiroView Answer on Stackoverflow
Solution 7 - Pythonuser5683940View Answer on Stackoverflow
Solution 8 - PythonJoy MukherjeeView Answer on Stackoverflow
Solution 9 - PythonJeffery WhiteView Answer on Stackoverflow
Solution 10 - Pythonsusan097View Answer on Stackoverflow
Solution 11 - PythonErikaView Answer on Stackoverflow
Solution 12 - PythonManasView Answer on Stackoverflow
Solution 13 - Pythonsunil_kumar_sahuView Answer on Stackoverflow
Solution 14 - PythonMuhammad Faizan FareedView Answer on Stackoverflow
Solution 15 - PythonJasonView Answer on Stackoverflow
Solution 16 - PythonLokendraView Answer on Stackoverflow
Solution 17 - PythonAly RadwanView Answer on Stackoverflow