39

This has been driving me crazy for the past 2 days. I installed virtualenv on my Macbook using pip install virtualenv. But when I try to create a new virtualenv using virtualenv venv, I get the error saying "virtualenv : command not found".

I used pip show virtualenv and the location of the installation is "Location: /usr/local/lib/python2.7/site-packages" but I can't figure out where the executable is. I tried dozens other similar looking posts but those solutions do not work for me.

Any ideas what might be going wrong here?

Pravesh Jain
  • 4,128
  • 6
  • 28
  • 47
  • Are you sure your python path is correct? Are you using the system installed python, or one you downloaded on your own? – OneCricketeer Oct 10 '16 at 18:38
  • Pythonpath wouldn't affect executing a program from the commandline... – John Gordon Oct 10 '16 at 18:39
  • 4
    Can you try `python -m venv`? – sytech Oct 10 '16 at 18:41
  • @cricket_007 : I've been using it for the past 2 years so I'm not sure but I think its the default installation. When I do `which python`, I get "/usr/local/bin/python" – Pravesh Jain Oct 10 '16 at 18:42
  • @Gator_Python: Yes when I do "python -m virtualenv venv" it creates the virtual environment. Thanks. But I wonder why this is not the default method to use virtualenv and still don't understand why the method listed in the docs doesn't work for me. – Pravesh Jain Oct 10 '16 at 18:45
  • `/usr/local` means you've downloaded your own python, which is separate from `/System/Library/Frameworks/Python.framework/`. If `virtualenv` cannot be found, then you are missing `/usr/local/bin/virtualenv` – OneCricketeer Oct 10 '16 at 18:50
  • @cricket_007 so what can I do to have an executable in /usr/local/bin/virtualenv ? – Pravesh Jain Oct 10 '16 at 19:34
  • 2
    I have python installed via homebrew. When I did `pip install virtualenv`, it created it there. – OneCricketeer Oct 10 '16 at 19:39
  • Possible duplicate of [How to add virtualenv to path](http://stackoverflow.com/questions/15453793/how-to-add-virtualenv-to-path) – vz0 Oct 11 '16 at 08:13
  • re: `pip install virtualenv` , I had to use `sudo` to get that command to succeed. – Cheeso Sep 19 '17 at 17:31
  • mine installed in a strange place Location: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages I used sudo pip3 install -U virtualenv # system-wide install Anybody know why I get virtualenv : command not found? – user1322114 May 26 '20 at 15:22

12 Answers12

94

The only workable approach I could figure out (with help from @Gator_Python was to do python -m virtualenv venv. This creates the virtual environment and works as expected.

I have custom python installed and maybe that's why the default approach doesn't work for me.

Pravesh Jain
  • 4,128
  • 6
  • 28
  • 47
20

On macOS Mojave
First check python is in the path.
python --version
Second check pip is installed.
pip --version
If it is not installed.
brew install pip
Third install virtualenv
sudo -H pip install virtualenv

Dr. Mian
  • 3,334
  • 10
  • 45
  • 69
17

For Python 3

python3 -m virtualenv venv
Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
11

As mentioned in the comments, you've got the virtualenv module installed properly in the expected environment since python -m venv allows you to create virtualenv's.

The fact that virtualenv is not a recognized command is a result of the virtualenv.py not being in your system PATH and/or not being executable. The root cause could be outdated distutils or setuptools.

You should attempt to locate the virtualenv.py file, ensure it is executable (chmod +x) and that its location is in your system PATH. On my system, virtualenv.py is in the ../Pythonx.x/Scripts folder, but this may be different for you.

sytech
  • 29,298
  • 3
  • 45
  • 86
  • The first solution worked for me, but the second failed, despite locating `virtualenv` in `~/lib/python/site-packages`, adding this directory to the `PATH`, and making `virtualenv.py` executable. I am on macOS Mojave with Python 3.6.7 and pip3. – miguelmorin Dec 11 '18 at 10:29
  • 1
    On macOS Mojave you can do `sudo -H pip install virtualenv` – Dr. Mian Jan 20 '19 at 21:38
1

Could it be that you are using Anaconda package manager? If so, then it has it's own virtual environment system which you setup as follows:

conda create --name venv
Bill
  • 10,323
  • 10
  • 62
  • 85
1

I had the same issue (although on ubuntu), a simple solution is instead of doing pip install virtualenv, you precede the commend with "sudo".

A little inspection reveals the reason behind this fix: enter image description here

pip install virtualenv tries to put an executable under /usr/local/bin so that it can be invoked from command line, but it failed because only root has write permission to that directory

an alternative is pip install --user virtualenv, here are some further readings 1,2

Community
  • 1
  • 1
watashiSHUN
  • 9,684
  • 4
  • 36
  • 44
1

Had the same problem on Windows. Command not found and can't find the executable in the directory given by pip show.
Fixed it by adding "C:\Users{My User}\AppData\Roaming\Python\Python39\Scripts" to the PATH environment variable.

Eric Pedley
  • 103
  • 9
1

Install virtualenv from https://pypi.org/project/virtualenv

python -m pip install --user virtualenv

sudo /usr/bin/easy_install virtualenv
Gopal Joshi
  • 2,350
  • 22
  • 49
0

I succeded creating manually a link to location/virtualenv.py in /usr/local/bin, naming it virtualenv and adding +x attribute on file

➜  ~ pip show virtualenv
Name: virtualenv
Version: 16.6.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb@colorstudy.com
License: MIT
Location: /home/prsadev/.local/lib/python2.7/site-packages
Requires: 


~ chmod +x /home/prsadev/.local/lib/python2.7/site-packages/virtualenv.py 
~ sudo ln -sf /home/prsadev/.local/lib/python2.7/site-packages/virtualenv.py /usr/local/bin/virtualenv
0

I tried to have virtualenv at a random location & faced the same issue on a UBUNTU machine, when I tried to run my 'venv'. What solved my issue was :-

$virtualenv -p python3 venv

Also,instead of using $activate try :- $source activate If you look at the activate script(or $cat activate), you will find the same in comment.

0

This solved my similar problem!

You need to look online on how to create a virtual environement with python X.X.X (replace x.x.x with your python version)

mine was python 3.4.3 so bellow is how should i deal with it:

sudo python3 -m venv aramisvenv
Aramis NSR
  • 1,602
  • 16
  • 26
0

I use asdf and had to do a reshim after installing virtualenv. asdf reshim

Fixed due to this response