bash: mkvirtualenv: command not found
Asked Answered
B

14

122

After following the instructions on Doug Hellman's virtualenvwrapper post, I still could not fire up a test environment.

[mpenning@tsunami ~]$ mkvirtualenv test
-bash: mkvirtualenv: command not found
[mpenning@tsunami ~]$

It should be noted that I'm using WORKON_HOME that is not in my $HOME. I tried looking for /usr/local/bin/virtualenvwrapper.sh as shown in the virtualenvwrapper installation docs, but it does not exist.

I'm running CentOS 6 and python 2.6.6, if this matters.


# File: ~/.bash_profile
# ...

export WORKON_HOME="/opt/virtual_env/"
source "/opt/virtual_env/bin/virtualenvwrapper_bashrc"
Brisling answered 13/12, 2012 at 8:3 Comment(0)
B
117

Solution 1:

For some reason, virtualenvwrapper.sh installed in /usr/bin/virtualenvwrapper.sh, instead of under /usr/local/bin.

The following in my .bash_profile works...

source "/usr/bin/virtualenvwrapper.sh"
export WORKON_HOME="/opt/virtual_env/"

My install seems to work fine without sourcing virtualenvwrapper_bashrc

Solution 2:

Alternatively as mentioned below, you could leverage the chance that virtualenvwrapper.sh is already in your shell's PATH and just issue a source `which virtualenvwrapper.sh`

Brisling answered 13/12, 2012 at 8:3 Comment(7)
Setting WORKON_HOME to "~/.virtualenvs" (default value) allows to set private virtualenvsSokotra
If you install using your distro's package manager, files will be in /usr/bin instead of /usr/local/bin. Generally, you should not assume one or the other to be found; that's one of the reasons we have a PATH variable.Missing
This answer was based on installing virtualenvwrapper with pip, which is what I prefer doing.Brisling
It needs to be installed first, pip install virtualenvwrapperPhira
WORKON_HOME is optional and has nothing to do with the solution.Emotionalism
After installing virtualwrapper it says no such file or directory found after the first line of your codeSyrinx
I found virtualenvwrapper/virtualenvwrapper.sh in /usr/share Ubuntu 20.04Dufresne
R
70

Try:

source `which virtualenvwrapper.sh`

The backticks are command substitution - they take whatever the program prints out and put it in the expression. In this case "which" checks the $PATH to find virtualenvwrapper.sh and outputs the path to it. The script is then read by the shell via 'source'.

If you want this to happen every time you restart your shell, it's probably better to grab the output from the "which" command first, and then put the "source" line in your shell, something like this:

echo "source /path/to/virtualenvwrapper.sh" >> ~/.profile

^ This may differ slightly based on your shell. Also, be careful not to use the a single > as this will truncate your ~/.profile :-o

Roughrider answered 7/7, 2016 at 12:57 Comment(3)
While this might be a valuable hint to solve the problem, an answer really needs a bit more detail than this. Please edit to explain how this will solve the problem. Alternatively, consider writing this as a comment instead.Interest
This solved my problem. But will someone explain why and how?Gower
The backticks are command substitution - they take whatever the program prints out and put it in the expression. In this case "which" checks the $PATH to find virtualenvwrapper.sh and outputs the path to it. The script is then read by the shell via 'source'.Roughrider
T
44

I had the same issue on OS X 10.9.1 with python 2.7.5. No issues with WORKON_HOME for me, but I did have to manually add source "/usr/local/bin/virtualenvwrapper.sh" to ~/.bash_profile (or ~/.bashrc in unix) after I ran pip install virtualenvwrapper

Tsarevitch answered 22/12, 2013 at 3:20 Comment(4)
Where did you manually add source "/usr/local/bin/virtualenvwrapper.sh"?Seaborne
@Seaborne I added that line to ~/.bash_profile. Note that when you first add it you'll either have to reload the terminal (which runs .bash_profile) or just run that source command directly from the command line.Tsarevitch
It would be very helpful if you had a title with the filename and displayed all the contents so you don't have to get followup questions about "where did you put it?"Individualism
Whether you use .bash_profile or .bashrc is not really directly a consequence of which platform you are usng, though one or the other may be missing on some platforms. See the Bash manual page for their differences. Generally, you should only really need this in your .bash_profile, but some distros use setups which complicate matters.Missing
P
28

Prerequisites to execute this command -

  1. pip (recursive acronym of Pip Installs Packages) is a package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI).

    sudo apt-get install python-pip

  2. Install Virtual Environment. Used to create virtual environment, to install packages and dependencies of multiple projects isolated from each other.

    sudo pip install virtualenv

  3. Install virtual environment wrapper About virtual env wrapper

    sudo pip install virtualenvwrapper

After Installing prerequisites you need to bring virtual environment wrapper into action to create virtual environment. Following are the steps -

  1. set virtual environment directory in path variable- export WORKON_HOME=(directory you need to save envs)

  2. source /usr/local/bin/virtualenvwrapper.sh -p $WORKON_HOME

As mentioned by @Mike, source `which virtualenvwrapper.sh` or which virtualenvwrapper.sh can used to locate virtualenvwrapper.sh file.

It's best to put above two lines in ~/.bashrc to avoid executing the above commands every time you open new shell. That's all you need to create environment using mkvirtualenv

Points to keep in mind -

  • Under Ubuntu, you may need install virtualenv and virtualenvwrapper as root. Simply prefix the command above with sudo.
  • Depending on the process used to install virtualenv, the path to virtualenvwrapper.sh may vary. Find the appropriate path by running $ find /usr -name virtualenvwrapper.sh. Adjust the line in your .bash_profile or .bashrc script accordingly.
Protasis answered 14/5, 2017 at 17:58 Comment(4)
Adding a note. On ubuntu 18.04, I had to reboot after install and then it worked.Ionosphere
@screenmutt Thanks for valuable input. I might have missed it as I use ubuntu 16.04. However, I would like to know after which step did you have to reboot ? Is is after installing pip packages or after setting up "virtual environment directory" ?Protasis
After the install of the wrapper. It worked after that.Ionosphere
@DanGrahn you don't need to reboot, just need to rerun .bashrc - either explicitly source ~/.bashrc in your current terminal, or open a new terminal windowBlinding
P
18

Use this procedure to create virtual env in ubuntu

Step 1

Install pip

   sudo apt-get install python-pip

step 2

Install virtualenv

   sudo pip install virtualenv

step 3

Create a dir to store your virtualenvs (I use ~/.virtualenvs)

   mkdir ~/.virtualenvs

or use this command to install specific version of python in env

virtualenv -p /usr/bin/python3.6 venv

step 4

   sudo pip install virtualenvwrapper

step 5

   sudo nano ~/.bashrc

step 6

Add this two line code at the end of the bashrc file

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

step 7

Open new terminal (recommended)

step 8

Create a new virtualenv

  mkvirtualenv myawesomeproject

step 9

To load or switch between virtualenvs, use the workon command:

  workon myawesomeproject

step 10

To exit your new virtualenv, use

 deactivate

and make sure using pip vs pip3

OR follow the steps below to install virtual environment using python3

Install env

python3 -m venv my-project-env

and activate your virtual environment using the following command:

source my-project-env/bin/activate

or if you want particular python version

virtualenv --python=python3.7.5 myenv
Pattani answered 22/2, 2019 at 19:45 Comment(0)
A
7

Since I just went though a drag, I'll try to write the answer I'd have wished for two hours ago. This is for people who don't just want the copy&paste solution

First: Do you wonder why copying and pasting paths works for some people while it doesn't work for others?** The main reason, solutions differ are different python versions, 2.x or 3.x. There are actually distinct versions of virtualenv and virtualenvwrapper that work with either python 2 or 3. If you are on python 2 install like so:

sudo pip install virutalenv
sudo pip install virtualenvwrapper

If you are planning to use python 3 install the related python 3 versions

sudo pip3 install virtualenv
sudo pip3 install virtualenvwrapper

You've successfully installed the packages for your python version and are all set, right? Well, try it. Type workon into your terminal. Your terminal will not be able to find the command (workon is a command of virtualenvwrapper). Of course it won't. Workon is an executable that will only be available to you once you load/source the file virtualenvwrapper.sh. But the official installation guide has you covered on this one, right?. Just open your .bash_profile and insert the following, it says in the documentation:

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

Especially the command source /usr/local/bin/virtualenvwrapper.sh seems helpful since the command seems to load/source the desired file virtualenvwrapper.sh that contains all the commands you want to work with like workon and mkvirtualenv. But yeah, no. When following the official installation guide, you are very likely to receive the error from the initial post: mkvirtualenv: command not found. Still no command is being found and you are still frustrated. So whats the problem here? The problem is that virtualenvwrapper.sh is not were you are looking for it right now. Short reminder ... you are looking here:

source /usr/local/bin/virtualenvwrapper.sh

But there is a pretty straight forward way to finding the desired file. Just type

which virtualenvwrapper

to your terminal. This will search your PATH for the file, since it is very likely to be in some folder that is included in the PATH of your system.

If your system is very exotic, the desired file will hide outside of a PATH folder. In that case you can find the path to virtalenvwrapper.sh with the shell command find / -name virtualenvwrapper.sh

Your result may look something like this: /Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh Congratulations. You have found your missing file!. Now all you have to do is changing one command in your .bash_profile. Just change:

source "/usr/local/bin/virtualenvwrapper.sh"

to:

"/Library/Frameworks/Python.framework/Versions/3.7/bin/virtualenvwrapper.sh"

Congratulations. Virtualenvwrapper does now work on your system. But you can do one more thing to enhance your solution. If you've found the file virtualenvwrapper.sh with the command which virtualenvwrapper.sh you know that it is inside of a folder of the PATH. So if you just write the filename, your file system will assume the file is inside of a PATH folder. So you you don't have to write out the full path. Just type:

source "virtualenvwrapper.sh"

Thats it. You are no longer frustrated. You have solved your problem. Hopefully.

Adulate answered 11/2, 2019 at 21:23 Comment(1)
I actually just copied and pased your solutionFayth
H
3

open your bashrc file using

gedit ~/.bashrc

go to the bottom of the bashrc file and copy these lines and paste it

which python3(gives you the python path)

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3


export WORKON_HOME=$HOME/.virtualenvs

which virtualenv (gives you the location of virtualenv)

export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv

which virtualenvwrapper.sh(returns the location of virtualenvwrapper.sh)

source /usr/local/bin/virtualenvwrapper.sh

please copy these lines in the same order as given above

for more information you can refer to:https://www.freecodecamp.org/news/virtualenv-with-virtualenvwrapper-on-ubuntu-18-04/

Alternatively you can use:

create virtual environment :

python3 -m venv <virtual environment name>

virtualenv <virtual environment name>

To activate the virtual environment

source <virtual environment name>/bin/activate

To deactivate the virtual environment

deactivate
Huskey answered 11/10, 2021 at 10:29 Comment(0)
P
2

Using Git Bash on Windows 10 and Python36 for Windows I found the virtualenvwrapper.sh in a slightly different place and running this resolved the issue

source virtualenvwrapper.sh 
/c/users/[myUserName]/AppData/Local/Programs/Python36/Scripts
Penrod answered 31/8, 2018 at 23:21 Comment(2)
Using Windows 10, Git Bash and Python 3.8, for me it was on: /c/Users/[myUserName]/AppData/Roaming/Python/Python38/ScriptsTooling
@Tooling thanks, it worked. On running source virtualenwrapper.sh , i wasn't able to see this path, when i did source /c/Users/[myUserName]/AppData/Roaming/Python/Python38/Scripts, mkvirtualenv <name> worked.Birdwell
C
2

In order to successfully install the virtualenvwrapper on Ubuntu 18.04.3 you need to do the following:

  1. Install virtualenv

    sudo apt install virtualenv
    
  2. Install virtualenvwrapper

    sudo pip install virtualenv
    sudo pip install virtualenvwrapper
    
  3. Add the following to the end of the .bashrc file

    export WORKON_HOME=~/virtualenvs
    export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python
    source ~/.local/bin/virtualenvwrapper.sh
    
  4. Execute the .bashrc file

    source ~/.bashrc
    
  5. Create your virtualenv

    mkvirtualenv your_virtualenv
    
Cliquish answered 25/2, 2020 at 14:41 Comment(1)
works on ubuntu 20.04 as well. a small correction tho: WORKON_HOME=~/.virtualenvs is the default. no need to set that. VIRTUALENVWRAPPER_PYTHON=/usr/bin/python is also the default, but when using python3 this is useful. also one should use export PATH="$HOME/.local/bin:$PATH" for the python binary pathsEmotionalism
F
1

On Windows 7 and Git Bash this helps me:

  1. Create a ~/.bashrc file (under your user home folder)
  2. Add line export WORKON_HOME=$HOME/.virtualenvs (you must create this folder if it doesn't exist)
  3. Add line source "C:\Program Files (x86)\Python36-32\Scripts\virtualenvwrapper.sh" (change path for your virtualenvwrapper.sh)

Restart your git bash and mkvirtualenv command now will work nicely.

Ferino answered 23/4, 2018 at 11:55 Comment(0)
V
1

On Debian

source /usr/share/virtualenvwrapper/virtualenvwrapper.sh
Vasomotor answered 31/3, 2022 at 11:52 Comment(0)
O
1

I dont know for what reason, but for me the virtualenvwrapper.sh file is located in /home/aditya/.local/bin and not in /usr/bin/ or /usr/local/bin.

Just added source ~/.local/bin/virtualenvwrapper.sh in my ~/.bashrc file and it worked well.

(Hope you know you should use bash command everytime you update your .bashrc .)

Ocampo answered 9/10, 2022 at 16:57 Comment(0)
C
0

Solved my issue in Ubuntu 14.04 OS with python 2.7.6, by adding below two lines into ~/.bash_profile (or ~/.bashrc in unix) files.

source "/usr/local/bin/virtualenvwrapper.sh"

export WORKON_HOME="/opt/virtual_env/"

And then executing both these lines onto the terminal.

Comedown answered 21/1, 2019 at 7:25 Comment(0)
C
0

On Windows 10, to create the virtual environment, I replace "pip mkvirtualenv myproject" by "mkvirtualenv myproject" and that works well.

Citizen answered 16/8, 2019 at 11:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.