Wednesday, February 4, 2015

Virtual Environment for Python

Although I heard of virtualenv before, I had hands on experience last weekend and thanks to my friend cum roommate, who recently joined a new company, for bringing it to me. Coming to the point, virtualenv is the tool to create virtual environment for python projects.

To install virtualenv use the following command,
pip install virtualenv
Now move to your current project folder and and create virtual environment using the following command.
virtualenv venv
The above command will create a folder in the current directory which will contain the python executable files.

You need to activate virtual environment before using it.
source venv/bin/activate
Once activated, we can install the packages required for the current project.
To take the snapshot of current state of the environment packages, use the following command.
pip freeze > requirements.txt
This will create a requirement.txt file, which contains a simple list of all the packages in the current environment and their respective versions. To use the same packages later, use the following command.
pip install -r requirements.txt
Use the following command to deactivate it.
deactivate

Happy Learning :)

No comments:

Post a Comment