virtual environment with virtualenvwrapper
Suppose you need to work on three different projects project A, project B and project C. project A and project B need python 3 and some required libraries. But for project C you need python 2.7 and dependent libraries.
So best practice for this is to separate those project environments. To create virtual environment you can use below technique:
Virtualenv, Virtualenvwrapper and Conda
Although we hav several options for virtual environment but virtualenvwrapper is most recommended.
Create virtual environment with virtualenvwrapper
Section titled “Create virtual environment with virtualenvwrapper”Suppose you need to work on three different projects project A, project B and project C. project A and project B need python 3 and some required libraries. But for project C you need python 2.7 and dependent libraries.
So best practice for this is to separate those project environments. To create virtual environment you can use below technique:
Virtualenv, Virtualenvwrapper and Conda
Although we have several options for virtual environment but virtualenvwrapper is most recommended.
Although we have several options for virtual environment but I always prefer virtualenvwrapper because it has more facility then 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.5Installingsetuptools.................................................................................................................................................................................done.virtualenvwrapper.user_scripts Creating /Users/salayhin/Envs/python_3.5/bin/predeactivatevirtualenvwrapper.user_scripts Creating /Users/salayhin/Envs/python_3.5/bin/postdeactivatevirtualenvwrapper.user_scripts Creating /Users/salayhin/Envs/python_3.5/bin/preactivatevirtualenvwrapper.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_HOMEpython_3.5 hook.logNow we can install some software into the environment.
(python_3.5)$ pip install djangoDownloading/unpacking djangoDownloading Django-1.1.1.tar.gz (5.6Mb): 5.6Mb downloadedRunning setup.py egg_info for package djangoInstalling collected packages: djangoRunning setup.py install for djangochanging mode of build/scripts-2.6/django-admin.py from 644 to 755changing mode of /Users/salayhin/Envs/env1/bin/django-admin.py to 755Successfully installed djangoWe can see the new package with lssitepackages:
(python_3.5)$ lssitepackagesDjango-1.1.1-py2.6.egg-info easy-install.pthsetuptools-0.6.10-py2.6.egg pip-0.6.3-py2.6.eggdjango setuptools.pthWe can create multiple virtual environment 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)$To exit the virtualenv
$ deactivate