Tag: Virtualenv

Set up Python Virtual Envs

No Comments

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.

  1. 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
  2. Python packages required – virtualenv, virtualenvwrapper.
    pip3 install virtualenv virtualenvwrapper  #pip3 is using python3 interpretor
  3. 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.
  4. 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

    PROJECT_HOME=’/Users/chetanak/Box Sync/Projects’

    #reload script
    source /usr/local/bin/virtualenvwrapper.sh

    #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
    export WORKON_HOME=$HOME/.virtualenvs

  5. Save the above file and run these commands

    mkdir ~/.virtualenvs

    #Load your ~/.bash_profile with the new changes.
    source ~/.bash_profile

  6. Now that all the paths and directories are created and set, you can create your virtual environments. Simply use –

    mkproject <projectname>

    #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

  7. To deactivate the virtualenv type ‘deactivate’ at the prompt
  8. If you want to know more about the virtualenvwrapper commands go to – http://docs.python-guide.org/en/latest/dev/virtualenvs/

Read More