346

I have been searching and tried various alternatives without success and spent several days on it now; it is driving me mad.

I am running on Red Hat Linux with Python 2.5.2. I began using the most recent Virtualenv, but I could not activate it. I found somewhere suggesting I needed an 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

python virtualenv-1.6.4/virtualenv.py virtual

Output:

New python executable in virtual/bin/python
Installing setuptools............done.
Installing pip...............done.

The environment looks ok

cd virtual
dir

Output:

bin  include  lib

Trying to activate

. bin/activate

Output:

/bin/.: Permission denied.

I checked chmod

cd bin
ls -l

Output:

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-

It was a problem, so I changed it

ls -l

Output:

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

Tring activate again

. bin/activate

Output:

/bin/.: Permission denied.

Still no joy...

4
  • 18
    have you tried "source ~/virtual/bin/activate"?
    – mmbrian
    Oct 2, 2013 at 10:05
  • 7
    From with the directory of your environment do . bin/activate and it should work fine.
    – dansalmo
    Feb 6, 2014 at 18:53
  • 6
    I think you just needed to use the source command, i.e. source bin/activate
    – Hack-R
    Apr 18, 2017 at 15:21
  • 1
    I think you have to do this from the virtualenv directory so first cd ~/.virtualenvs/*environmentname* then source ~/.virtual/bin/activate
    – 11m0
    Mar 12, 2019 at 2:02

18 Answers 18

420

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

virtualenv venv --distribute

Output:

New python executable in venv/bin/python
Installing distribute.........done.
Installing pip................done.

And

source venv/bin/activate
python
9
  • 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, 2013 at 16:23
  • 10
    Don't type the $ character, which ii looks like you did here: [necrailk@server6 ~]$ $source. $ is used to indicate a command prompt. Feb 4, 2013 at 14:31
  • 3
    Still getiing badly placed ()'s error...what to do?
    – user1733583
    Feb 9, 2014 at 5:46
  • 3
    same error here :( user@mintdesk ~/python_v $ venv/bin/activate bash: venv/bin/activate: Permission denied
    – Rui Lima
    Mar 17, 2014 at 13:34
  • 1
    An alternative to typing 'source' all the time is to use a '.': $ . venv/bin/activate
    – karwag
    Dec 25, 2015 at 16:44
131

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

There isn't any need to make activate executable via chmod.

5
  • 9
    OP typed . bin/activate. Note the ., which is a synonym for source.
    – Thomas
    Feb 21, 2018 at 8:15
  • 2
    "source is an executable name"... which executable? cmd.exe? powershell.exe?
    – StingyJack
    Apr 11, 2019 at 16:08
  • 1
    I'm trying to activate an environment from cron, and it wouldn't work without being executable (chmod 755).
    – Tahlor
    Jan 26, 2020 at 16:30
  • activate is just a script and its first two lines say: 1 # This file must be used with "source bin/activate" from bash 2 # you cannot run it directly". so I think this should be the accepted answer Aug 22, 2022 at 9:46
  • In a cron job you'd want to run the whole thing via bash -c. A virtual environment has no meaning outside a shell, so you need to run the shell and pass it a command line that changes to the correct directory and activates the virtualenv, then does whatever else needs doing in the context of the virtualenv. Might be easier to write a short script to do all that if it's at all complex.
    – kindall
    Feb 28 at 17:15
106

You can do

source ./python_env/bin/activate

Or just go to the directory:

cd /python_env/bin/

and then

source ./activate
42

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

31

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

Now type

source activate
22

mkdir <YOURPROJECT>: Create a new project

cd <YOURPROJECT>: Change directory to that project

virtualenv <NEWVIRTUALENV>: Creating a new virtualenv

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

22

Run this code. It will get activated if you are on a Windows machine:

. venv/Scripts/activate

Run this code. It will get activated if you are on a Linux or Mac machine:

. venv/bin/activate

4
  • it worked with env instead venv on linux for me Nov 1, 2021 at 14:13
  • yeah that's good then your venv name is might be env thats why. Dec 25, 2021 at 13:34
  • 1
    @VitaliyTerziev - in your virtual environment must have been create as env not venv. Jul 13, 2022 at 18:34
  • 1
    Please review Why not upload images of code/errors when asking a question? (e.g., "Images should only be used to illustrate problems that can't be made clear in any other way, such as to provide screenshots of a user interface.") and do the right thing (it covers answers and program input/output as well). Thanks in advance. Feb 28 at 17:31
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.

4
  • 2
    Also, you should source bin/activate, not .bin/activate. Might even work with the . then...
    – krlmlr
    Jan 30, 2013 at 13:16
  • @krlmlr True, I had assumed that was a typo, since in the next line he cds into bin.
    – kojiro
    Jan 30, 2013 at 13:17
  • 1
    hi[necrailk@server6 ~/virtual]$ source bin/activate Badly placed ()'s. [necrailk@server6 ~/virtual]$
    – larry
    Jan 30, 2013 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 8, 2013 at 17:53
16

Instead of ./activate, use source activate:

See this screenshot

1
12

For Windows, you can perform it as:

To create the virtual environment as: virtualenv envName –python=python.exe (if not, create an environment variable)

To activate the virtual environment: \path\to\envName\Scripts\activate

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

It works fine on the new Python version.

2
  • 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 2, 2019 at 23:42
  • '.' is not recognized as an internal or external command, operable program or batch file. Aug 7, 2020 at 23:11
5

Windows 10

In Windows, these directories are created:

Windows 10 virtual environment directories

To activate the virtual environment in Windows 10.

down\scripts\activate

The \scripts directory contains the activate file.

Linux Ubuntu

In Ubuntu, these directories are created:

Linux Ubuntu virtual environment directories

To activate the virtual environment in Linux Ubuntu.

source ./bin/activate

The /bin directory contains the activate file.


The virtual environment can be copied from Windows to Linux Ubuntu and vice versa

If the virtual environment folder is 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.

4
  • hi source and . (period) seem interchangable necrailk@server6 ~/virtual]$ source bin/activate Badly placed ()'s. this does not seem to work
    – larry
    Jan 30, 2013 at 14:22
  • my bin permission: drwxrwxr-x 2 erika erika 4096 2013-01-24 14:37 bin/
    – Erika
    Jan 30, 2013 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, 2013 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 8, 2013 at 17:50
3

Create your own Python virtual environment called <your environment _name>:.

I have given it the 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 Virtualenv repository.

1
  • 2
    You shouldn't be checking out the master branch of virtualenv and expecting it to actually work. Use a tagged version instead. Sep 26, 2017 at 19:35
3

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

. venv/bin/activate

.: Command not found.

source venv/bin/activate

Badly placed ()'s.

bash
source venv/bin/activate

New prompt:

(venv) bash-3.2$

Bingo, it worked. See, the prompt changed.

On Ubuntu:

source toolsenv/bin/activate

New prompt:

(toolsenv) user@local_host~/tools$

Note: the prompt changed

1
  • Works in zsh too, atleast now
    – Suraj
    Jul 6, 2022 at 1:47
2

I had trouble getting source /bin/activate running, but then I realized I was using tcsh as my terminal shell instead of Bash.

Once I switched, I was able to activate venv.

1

I was getting this error "-sh: 13: source: not found"

Resolution is to first login as sudo

  1. $sudo su

Then execute the command

  1. #source virtual_env_name/bin/activate
0

I had faced the same problem. The main reason being that I created the virtualenv as a "root" user. But later I 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 example, 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.

0
  1. Open a PowerShell window and navigate to your application folder
  2. Enter your virtualenv folder, for example: cd .\venv\Scripts\
  3. Activate the virtual environment by typing .\activate

Not the answer you're looking for? Browse other questions tagged or ask your own question.