After following the instructions on Doug Hellman\'s virtualenvwrapper post, I still could not fire up a test environment.
[mpenning@tsunami ~]$ mkvirtualenv
Prerequisites to execute this command -
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
Install Virtual Environment. Used to create virtual environment, to install packages and dependencies of multiple projects isolated from each other.
sudo pip install virtualenv
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 -
set virtual environment directory in path variable-
export WORKON_HOME=(directory you need to save envs)
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 -
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
On Windows 10, to create the virtual environment, I replace "pip mkvirtualenv myproject" by "mkvirtualenv myproject" and that works well.
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
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
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`