Create virtual environment with virtualenvwrapper
suggest changeThe best practice is to separate those project environments. To create a virtual environment you can use the techniques below:
Virtualenv, Virtualenvwrapper and Conda
Although there are several options for a virtual environment, virtualenvwrapper is the most recommended.
Although there are several options for a virtual environment, I always prefer virtualenvwrapper because it has more facilities than the others.
$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ mkdir -p $WORKON_HOME
$ source /usr/local/bin/virtualenvwrapper.sh
$ printf '\n%s\n%s\n%s' '# virtualenv' 'export WORKON_HOME=~/virtualenvs' 'source /home/salayhin/bin/virtualenvwrapper.sh' >> ~/.bashrc
$ source ~/.bashrc
$ mkvirtualenv python_3.5
Installing
setuptools..........................................
....................................................
....................................................
...............................done.
virtualenvwrapper.user_scripts Creating /Users/salayhin/Envs/python_3.5/bin/predeactivate
virtualenvwrapper.user_scripts Creating /Users/salayhin/Envs/python_3.5/bin/postdeactivate
virtualenvwrapper.user_scripts Creating /Users/salayhin/Envs/python_3.5/bin/preactivate
virtualenvwrapper.user_scripts Creating /Users/salayhin/Envs/python_3.5/bin/postactivate New python executable in python_3.5/bin/python
(python_3.5)$ ls $WORKON_HOME
python_3.5 hook.log
The shell setup writes WORKON_HOME=~/virtualenvs and sources /home/salayhin/bin/virtualenvwrapper.sh. The earlier lines use ~/Envs and /usr/local/bin/virtualenvwrapper.sh. The log uses /Users/salayhin/Envs. virtualenvwrapper was not run here.
Now we can install some software into the environment.
(python_3.5)$ pip install django
Downloading/unpacking django
Downloading Django-1.1.1.tar.gz (5.6Mb): 5.6Mb downloaded
Running setup.py egg_info for package django
Installing collected packages: django
Running setup.py install for django
changing mode of build/scripts-2.6/django-admin.py from 644 to 755
changing mode of /Users/salayhin/Envs/env1/bin/django-admin.py to 755
Successfully installed django
This log installs Django 1.1.1 and mentions scripts-2.6 and the directory env1, not python_3.5. It was not rerun.
We can see the new package with lssitepackages:
The package listing is not shown.
We can create multiple virtual environments if we want.
Switch between environments with workon:
(python_3.6)$ workon python_3.5
(python_3.5)$ echo $VIRTUAL_ENV
/Users/salayhin/Envs/env1
(python_3.5)$
The prompt before workon is python_3.6, and $VIRTUAL_ENV prints /Users/salayhin/Envs/env1, not python_3.5.
To exit the virtualenv
$ deactivate