Tag Archives: virtualenv

Getting Started with virtualenv


Why should you care about using virtualenv?  Basically it provides you with a development environment for your various python projects.  Not all python projects will call for the same libraries and being able to solely keep track of the needed libraries within a specific environment can make life easier by:

  • Reducing the # of dependencies that need to be maintained in a single environment
  • Reduce the risk of breaking program dependencies of versioned libraries
  • Replicate the needed environment in other machines without re-installing every single library that you may have done once upon a time

Here are some simple steps to get started with virtualenv on a brand new machine (should apply to both linux and os x)

On a new installation of Linux (or OS X instance)
1.) sudo apt-get install pip #install pip so you can install virtualenv
2.) sudo pip install virtualenv #install virtualenv so you can have controlled python environments

 
3.) virtualenv new_project  #create a project directory and execute this command where new_project = your project name for e.g.

 
4.) source new_project/bin/activate #to start your virtual environment
5.) pip install psycopg2 #install any python library in your new python virtual environment

 

 
6.) pip install jupyter #installs jupyter notebooks! (which can be kicked off on your browser)

 

 

 

7.) deactivate #when you are done working in your virtual environment, deactivate

 

From here, you can return to step 4 when you want to work on your project again. Or go to step 3 if you want to start a new project.

To capture your python virtual environment components, do the following:
pip freeze > requirements.txt

This way, if you need to recreate your environment, you can use the file by typing:
pip install -r requirements.txt

For more info, go here: http://python-guide-pt-br.readthedocs.io/en/latest/dev/virtualenvs/

Before I go, if you are wondering how to start jupyter in your web browser within your new virtualenv, via the command line execute the following:

source my_project/bin/activate #assuming you exited from your virtualenv

jupyter-notebook #your web browser should open after this

 

 

Advertisements

Leave a comment

Filed under Linux, Python