Thursday, July 6, 2017

Setting up virtual environment (virtualenv) for python app development

When working on fairly large app in python, it is a good practice to create/setup an isolated python environment (called virtual environment - virtualenv) for development.

There are many long term reason for setting up a python's virtual environment (virtualenv) for any project you are working on. The notable once include:
1) When you have to experiment with something new.
2) When you want to use two different versions of package and compare them.
3) Virtualenv really shines when you have a number of projects, and don't want them to all share the same Python installation. For example, you could have two project with conflicting requirements.
4) Virtualenv will let you define isolated Python runtime environments without harming the root or system python installation. This comes in handy when deploying several python applications and you want to isolate the different environments. Typically this is needed when runtime dependencies differ between frameworks or libraries in different applications.

Creating a virtual environment

Step 1: Create a folder where you want to have the python virtual environment
Step 2: Open CMD or terminal window in that folder
Step 3: Enter "virtualenv name_of_folder" on the cmd/terminal

At this point, you did have a virtual environment created.


Using a virtual environment

Step 1: Open CMD or terminal window in that folder
Step 2: Enter "folder_name\Scripts\activate"
Step 3: To deactivate, simply enter "deactivate"
Step 4: To install packages enter "pip install package_name"
Step 5: To view the install packages enter "pip freeze"
Step 6: To save the install packages to file, enter "pip freeze > file_name.txt"



That is it!

No comments:

Post a Comment