Virtualenv Command Not Found

PythonMacosVirtualenv

Python Problem Overview


I couldn't get virtualenv to work despite various attempts. I installed virtualenv on MAC OS X using:

pip install virtualenv

and have also added the PATH into my .bash_profile. Every time I try to run the virtualenv command, it returns:

-bash: virtualenv: command not found

Every time I run pip install virtualenv, it returns:

Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages

I understand that in mac, the virtualenv should be correctly installed in

/usr/local/bin

The virtualenv is indeed installed in /usr/local/bin, but whenever I try to run the virtualenv command, the command is not found. I've also tried to run the virtualenv command in the directory /usr/local/bin, and it gives me the same result:

-bash: virtualenv: command not found

These are the PATHs I added to my .bash_profile

export PATH=$PATH:/usr/local/bin
export PATH=$PATH:/usr/local/bin/python
export PATH=$PATH:/Library/Framework/Python.framework/Version/2.7/lib/site-packages

Any workarounds for this? Why is this the case?

Python Solutions


Solution 1 - Python

If you installed it with

pip install virtualenv

You need to run

sudo /usr/bin/easy_install virtualenv

which puts it in /usr/local/bin/.

The above directory by default should be in your PATH; otherwise, edit your .zshrc (or .bashrc) accordingly.

Solution 2 - Python

I faced the same issue and this is how I solved it:

  1. The issue occurred to me because I installed virtualenv via pip as a regular user (not root). pip installed the packages into the directory ~/.local/lib/pythonX.X/site-packages
  2. When I ran pip as root or with admin privileges (sudo), it installed packages in /usr/lib/pythonX.X/dist-packages. This path might be different for you.
  3. virtualenv command gets recognized only in the second scenario
  4. So, to solve the issue, do pip uninstall virtualenv and then reinstall it with sudo pip install virtualenv (or install as root)

Solution 3 - Python

The simplest answer. Just:

pip uninstall virtualenv

and then:

pip install virtualenv

Or you maybe installed virtualenv with sudo, in that case:

pip install --user virtualenv

Solution 4 - Python

On Ubuntu 18.04 LTS I also faced same error. Following command worked:

sudo apt-get install python-virtualenv

Solution 5 - Python

python3 -m virtualenv virtualenv_name   

or

python -m virtualenv virtualenv_name

Solution 6 - Python

I had same problem on Mac OS X El Capitan.

When I installed virtualenv like that sudo pip3 install virtualenv I didn't have virtualenv under my command line.

I solved this problem by following those steps:

  1. Uninstall previous installations.
  2. Switch to super user account prior to virtualenv installation by calling sudo su
  3. Install virtualenv by calling pip3 install virtualenv
  4. Finally you should be able to access virtualenv from both user and super user account.

Solution 7 - Python

I had the same issue. I used the following steps to make it work

sudo pip uninstall virtualenv

sudo -H pip install virtualenv

That is it. It started working.

Usage of sudo -H----> sudo -H: set HOME variable to target user's home dir.

Solution 8 - Python

Figure out the problem

Try installing with the --verbose flag

pip install virtualenv --verbose

Output will look something like this

  ..
  Using cached virtualenv-15.1.0-py2.py3-none-any.whl
  Downloading from URL https://pypi.python.org/packages/6f/86/3dc328ee7b1a6419ebfac7896d882fba83c48e3561d22ddddf38294d3e83/virtualenv-15.1.0-py2.py3-none-any.whl#md5=aa7e5b86cc8cdb99794c4b99e8d670f3 (from https://pypi.python.org/simple/virtualenv/)
Installing collected packages: virtualenv

  changing mode of /home/manos/.local/bin/virtualenv to 755
Successfully installed virtualenv-15.1.0
Cleaning up...

From the output we can see that it's installed at /home/manos/.local/bin/virtualenv so let's ensure PATH includes that.

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

In my case we can clearly see that /home/manos/.local/bin is totally missing and that's why the shell can't find the program.

Solutions

We can solve this in many ways:

  1. We can install directly to a specific directory by fiddling with pip options (not recomended).
  2. Create appropriate symlinks at /usr/local/bin or similar.
  3. Append /home/manos/.local/bin to PATH.
  4. Install as sudo to install directly to /usr/local/bin

The two last options are probably the most sensible. The last solution is the simplest so therefore I will just show solution 3.

Add this to ~/.profile:

PATH="$PATH:$HOME/.local/bin"

Logout out and in again and it should work.

Solution 9 - Python

Found this solution and this worked perfectly for me.

sudo -H pip install virtualenv

The -H sets it to the HOME directory, which seems to be the issue for most people.

Solution 10 - Python

Personally. I did the same steps you did on a fresh Ubuntu 20 installation (except that I used pip3). I got the same problem, and I remember I solved it this way:

python3 -m virtualenv venv 

Link to understand the -m <module-name> notation.

Solution 11 - Python

In my case, I ran pip show virtualenv to get the information about virtualenv package. I will look similar to this and will also show location of the package:

user@machine:~$ pip show virtualenv
Name: virtualenv
Version: 16.2.0
Summary: Virtual Python Environment builder
Home-page: https://virtualenv.pypa.io/
Author: Ian Bicking
Author-email: ianb@colorstudy.com
License: MIT
Location: /home/user/.local/lib/python3.6/site-packages
Requires: setuptools

From that grab the part of location up to the .local part, which in this case is /home/user/.local/. You can find virtualenv command under /home/user/.local/bin/virtualenv.

You can then run commands like /home/user/.local/bin/virtualenv newvirtualenv.

Solution 12 - Python

You said that every time you run the pip install you get Requirement already satisfied (use --upgrade to upgrade): virtualenv in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages. What you need to do is the following:

  1. Change Directory (go to to the one where the virtualenv.py) cd /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages
  2. If you do an ls you will see that the script is there virtualenv.py
  3. Run the script like this: python virtualenv.py --distribute /the/path/at/which/you/want/the/new/venv/at theNameOfTheNewVirtualEnv

Hope this helps. My advice would be to research venvs more. Here is a good resource: https://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/

Solution 13 - Python

I had troubles because I used apt to install python-virtualenv package. To get it working I had to remove this package with apt-get remove python-virtualenv and install it with pip install virtualenv.

Solution 14 - Python

Ensure that virtualenv is executable.

If virtualenv is not found, running the full path (/usr/local/bin/virtualenv) should work.

Solution 15 - Python

I think your problem can be solved using a simple symbolic link, but you are creating the symbolic link to the wrong file. As far as I know virtualenv is installed to /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv, (you can change the numbers for your Python version) so the command for creating the symbolic link should be:

ln -s /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenv /usr/local/bin/virtualenv

Solution 16 - Python

I had the same problem for a long time. I solved it by running these two commands, first is to install second is to activate the env:

python3 -m pip install virtualenv
python3 -m virtualenv yourenvname

Note that I'm using python3, you can change it to just python if python3 fails. Thanks.

Solution 17 - Python

On ubuntu 18.4 on AWS installation with pip don't work correctly. Using apt-get install the problem was solved for me.

sudo apt-get install python-virtualenv

and to check

virtualenv --version

Solution 18 - Python

Same problem: So I just did pip uninstall virtualenv Then pip install virtualenv

pip install virtualenv --user

Collecting virtualenv Using cached https://files.pythonhosted.org/packages/b6/30/96a02b2287098b23b875bc8c2f58071c35d2efe84f747b64d523721dc2b5/virtualenv-16.0.0-py2.py3-none-any.whl Installing collected packages: virtualenv

Then I got this : > The script virtualenv is installed in '/Users/brahim/Library/Python/2.7/bin' which is not on PATH. Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.

which clearly says where it is installed and what to do to get it

Solution 19 - Python

If you're using Linux, open your terminal and type virtualenv halfway and autocomplete with tab key. If there's no auto-completion install virtualenv on your system by running:

mycomp$sudo apt-get install virtualenv
//if you're already super user.
mycomp#apt-get install virtualenv

You can now navigate to where you want to create your project and do:

myprj$pip3 install virtualenv    
//to install python 3.5 and above  
myprj$virtualenv venv --python=python3.5  
//to activate virtualenv  
(venv)myprj$source venv/bin/activate  
(venv)myprj$deactivate

Solution 20 - Python

Make sure that you are using

sudo

In this case, at first you need to uninstall the pipenv and then install again using sudo command.

  1. pip uninstall pipenv
  2. sudo pip install pipenv

Solution 21 - Python

You are having this error :

zsh: command not found: virtualenv

Because most probably you tried to install virtualenv without typing sudo beforehand.

If you try to add it to /usr/local/bin, this may result on syntax errors as the packages are not properly isntalled/copied:

SyntaxError: invalid syntax

  File "build/bdist.macosx-12.0-x86_64/egg/platformdirs/__main__.py", line 16
    def main() -> None:
               ^

In case you have tried to install virtualenv via pip without sudo rights, you need first to uninstall it:

pip3 uninstall virtualenv

Then install it using sudo:

sudo pip3 install virtualenv

Next you just need to activate the env:

virtualenv env 
source env/bin/activate 

Solution 22 - Python

Follow these basic steps to setup the virtual env

sudo pip install virtualenv virtualenvwrapper
sudo rm -rf ~/get-pip.py ~/.cache/pip

we need to update our ~/.bashrc

export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

The ~/.bashrc file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called WORKON_HOME to point to the directory where our Python virtual environments live. We then load any necessary configurations from virtualenvwrapper .

To update your ~/.bashrc file simply use a standard text editor, nano is likely the easiest to operate. A more simple solution is to use the cat command and avoid editors entirely:

echo -e "\n# virtualenv and virtualenvwrapper" >> ~/.bashrc
echo "export WORKON_HOME=$HOME/.virtualenvs" >> ~/.bashrc
echo "source /usr/local/bin/virtualenvwrapper.sh" >> ~/.bashrc

After editing our ~/.bashrc file, we need to reload the changes:

source ~/.bashrc

Now that we have installed virtualenv and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv command.

mkvirtualenv YOURENV

Solution 23 - Python

I'm doing Angela Yu's online iOS course and I was getting same problem plus also was getting permission denied error 13 when I was trying to run virtualenv --python=/{myPath} {newVirtualEnvName}

I solved it by:

  1. switching to sudo user sudo su
  2. navigating to my destination folder (where I want my new virtual env to live) ie. /Users/muUserName/Environments/
  3. run command python -m virtualenv python27 where python27 is a name of my new virtual environment
  4. above created folder pathon27 in my Environments folder, and then I was able to run source python27/bin/activate to start my virtualenv

Solution 24 - Python

this works in ubuntu 18 and above (not tested in previous versions):

sudo apt install python3-virtualenv

Solution 25 - Python

For me it was installed in this path (python 2.7 on MacOS): $HOME/Library/Python/2.7/bin

Solution 26 - Python

Simple answer is that if you are not a sudo user as I was not one.You need to add path of your bin folder (/home/myusername/.local/bin).So basically the command line searches in which of these path is the command which you have typed.

export PATH=/home/b18150/.local/bin:/usr/bin:/bin

here it will search in local/bin first then /usr/bin and then /bin.

Solution 27 - Python

apt update
apt upgrade
apt install ufw python virtualenv git unzip pv

3 commands and everything working!

Solution 28 - Python

sudo apt-get install python-virtualenv

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
QuestionArialView Question on Stackoverflow
Solution 1 - PythonTal AvissarView Answer on Stackoverflow
Solution 2 - PythonvettipayyanView Answer on Stackoverflow
Solution 3 - PythonLuis MartinsView Answer on Stackoverflow
Solution 4 - PythonsamView Answer on Stackoverflow
Solution 5 - PythonkhannikkeyView Answer on Stackoverflow
Solution 6 - PythonKrystian SakowskiView Answer on Stackoverflow
Solution 7 - PythonAbbyView Answer on Stackoverflow
Solution 8 - PythonPithikosView Answer on Stackoverflow
Solution 9 - PythonLuisView Answer on Stackoverflow
Solution 10 - PythonBillal BegueradjView Answer on Stackoverflow
Solution 11 - PythonDejvView Answer on Stackoverflow
Solution 12 - PythonBouramasView Answer on Stackoverflow
Solution 13 - PythonBoris DavidovView Answer on Stackoverflow
Solution 14 - PythonVertigoRayView Answer on Stackoverflow
Solution 15 - PythonBobby Wan-KenobiView Answer on Stackoverflow
Solution 16 - PythonDev_mjmView Answer on Stackoverflow
Solution 17 - PythonDAmeView Answer on Stackoverflow
Solution 18 - PythonSiempayView Answer on Stackoverflow
Solution 19 - Python7guyoView Answer on Stackoverflow
Solution 20 - PythonMd Samiul IslamView Answer on Stackoverflow
Solution 21 - PythonYoussefView Answer on Stackoverflow
Solution 22 - Pythonakki09View Answer on Stackoverflow
Solution 23 - Pythonmarika.dabojaView Answer on Stackoverflow
Solution 24 - PythonCyanBookView Answer on Stackoverflow
Solution 25 - PythonRavish BhagdevView Answer on Stackoverflow
Solution 26 - PythonKaran DoshiView Answer on Stackoverflow
Solution 27 - PythonArt RaptorsView Answer on Stackoverflow
Solution 28 - Pythonstray.leoneView Answer on Stackoverflow