Set up Python Virtual Envs
Developing an application in its own virtualenv is vital. Virtual envs help isolate python envrionments and enable you to install dependant packages in this isolated environment rather than globally.
- First find out the version of python you installed. I have python 2.7 default install on MacOS and python 3 via Homebrew package manager for MacOS
- Python packages required – virtualenv, virtualenvwrapper.
pip3 install virtualenv virtualenvwrapper #pip3 is using python3 interpretor
- Virtualenv package enables you to create project/application specific virtual env. Virtualenvwrapper is an extension of Virtualenv. It helps create,manage and delete virtual envs making it easier to work on more than one project at a time eliminating conflicts.
- Add the below lines to the ~/.bash_profile
#tells which interpretor to use. In this case ‘python3’
#set the env variable and assign a path to create projects
#Exports the varible and makes available the previous and the new env to all the processes. All the virtual envs are stored under ‘.virtualenvs’ folder in your home directory
- Save the above file and run these commands
#Load your ~/.bash_profile with the new changes.
- Now that all the paths and directories are created and set, you can create your virtual environments. Simply use –
#This creates a virtual env with the name of the project under ~/.virtualenvs directory and also create the project directory with the same name under $PROJECT_HOME. Since we are using the virtualenvwrapper functions, upon creation, it will activate the virtual env and ‘cd’ into the project folder
- To deactivate the virtualenv type ‘deactivate’ at the prompt
- If you want to know more about the virtualenvwrapper commands go to – http://docs.python-guide.org/en/latest/dev/virtualenvs/