312

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

Kai - Kazuya Ito
  • 6,454
  • 5
  • 44
  • 50
larry
  • 3,129
  • 2
  • 12
  • 5

17 Answers17

374

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
kqw
  • 19,513
  • 11
  • 64
  • 96
topherjaynes
  • 3,763
  • 1
  • 12
  • 3
  • 4
    ok, tried this no joy [necrailk@server6 ~/virtual]$ cd [necrailk@server6 ~]$ $source virtual/bin/activate source: Undefined variable. [necrailk@server6 ~]$ sh virtual/bin/activate [necrailk@server6 ~]$ – larry Jan 31 '13 at 16:23
  • 9
    Don't type the `$` character, which ii looks like you did here: `[necrailk@server6 ~]$ $source`. `$` is used to indicate a command prompt. – Justin Garrick Feb 04 '13 at 14:31
  • 3
    Still getiing badly placed ()'s error...what to do? –  Feb 09 '14 at 05:46
  • 2
    same error here :( user@mintdesk ~/python_v $ venv/bin/activate bash: venv/bin/activate: Permission denied – Rui Lima Mar 17 '14 at 13:34
  • 1
    An alternative to typing 'source' all the time is to use a '.': `$ . venv/bin/activate` – karwag Dec 25 '15 at 16:44
  • Use activate.csh instead of activate – zmbq Jan 14 '16 at 09:56
  • what if there is no /bin/activate file? – Ali Almoullim Feb 25 '18 at 14:26
  • @user1733583 I got the same error, I think the script is a bash file, so make sure you are running it in bash. I receive the error using cshell – mjsr Oct 31 '18 at 17:55
  • "--distribute DEPRECATED. Retained only for backward compatibility" is shown to me using "virtualenv --help", version 16.6.0. Thus, not necessary anymore?! – gebbissimo Jun 26 '19 at 07:19
118

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.

kqw
  • 19,513
  • 11
  • 64
  • 96
95

You can do

source ./python_env/bin/activate

or just go to the directory

cd /python_env/bin/

and then

source ./activate

Good Luck.

cquptzzq
  • 961
  • 6
  • 3
39

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

GNK
  • 665
  • 8
  • 13
30

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
Reihan_amn
  • 2,495
  • 2
  • 19
  • 19
21

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.

Community
  • 1
  • 1
kojiro
  • 71,091
  • 17
  • 131
  • 188
  • 2
    Also, you should source `bin/activate`, not `.bin/activate`. Might even work with the `.` then... – krlmlr Jan 30 '13 at 13:16
  • @krlmlr True, I had assumed that was a typo, since in the next line he `cd`s into `bin`. – kojiro Jan 30 '13 at 13:17
  • 1
    hi[necrailk@server6 ~/virtual]$ source bin/activate Badly placed ()'s. [necrailk@server6 ~/virtual]$ – larry Jan 30 '13 at 14:21
  • 2
    korjiro - you were correct - i did need to find out why environment was odd - it turned out to be a non standard bash implementation- switching to standard bash solved all of the problems – larry Feb 08 '13 at 17:53
21

$ 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

16

instead of ./activate

use source activate

See this screenshot

Paul Roub
  • 35,848
  • 27
  • 79
  • 88
Joy Mukherjee
  • 163
  • 1
  • 6
16

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

Jeffery White
  • 198
  • 1
  • 7
12

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 .

susan097
  • 2,960
  • 1
  • 21
  • 28
  • 1
    To add clarifying details to this: once you create your virtual environment with `virtualenv venv `, then manually go into the Scripts folder that was created just to look at the files, you'll see some activate files. So that is where we activate it for Windows. So `cd` into your Scripts folder and type `. activate` into your command line (be sure to include a space after the period). You'll notice your path in the command line changes, by adding (venv) to the beginning of your path. This means it's now activated. – Azurespot Dec 02 '19 at 23:42
  • '.' is not recognized as an internal or external command, operable program or batch file. – youHaveAlsoBeenABeginner Aug 07 '20 at 23:11
5

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
4

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/

Erika
  • 885
  • 1
  • 8
  • 15
  • hi source and . (period) seem interchangable necrailk@server6 ~/virtual]$ source bin/activate Badly placed ()'s. this does not seem to work – larry Jan 30 '13 at 14:22
  • my bin permission: _drwxrwxr-x 2 erika erika 4096 2013-01-24 14:37 bin/_ – Erika Jan 30 '13 at 15:16
  • according to the docs virtuallenvwrapper currently is not tested for python2.5 so not comfortable adding to my possible problems- but thanks – larry Jan 31 '13 at 16:37
  • Just a roundup. discovered my host was using a non standard shell. When changed to normal Bash everything worked as it should. now displays virtual env in the prompt and all paths are modified accordinglt. Many thanks for the help and suggestions. – larry Feb 08 '13 at 17:50
3

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.

bruntime
  • 371
  • 2
  • 13
Manas
  • 61
  • 1
  • 2
    You shouldn't be checking out the master branch of virtualenv and expecting it to actually work. Use a tagged version instead. – Jon Bringhurst Sep 26 '17 at 19:35
3

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

2

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.

Jason
  • 21
  • 1
0

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.

Lokendra
  • 69
  • 1
  • 5
0

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

Aly Radwan
  • 201
  • 3
  • 6