Python – Working With Virtual Environments

When you work with python for web development, data analysis or other solution , you need to install packages from pip and sometimes we have conflicts: Web framework X needs one package  version 1.2 and Web framework Y needs the same package but version 1.4. 

One Great solution for that problem is virtual environment – a complete environment with everything you need in one folder tree. You can simply copy it from one computer to another or delete the folder and everything is uninstalled.

 

Creating a Virtual Environment

Make sure you have python installed (and pip)

Installing virtual environment:

# pip install virtualenv

Creating a new environment :

# virtualenv -p [PATH to python executable] [target directory]

So if for example, python3 interpreter is located in /usr/bin and we want to create a directory myenv for our virtual environment :

# virtualenv -p /usr/bin/python3 myenv

cd into the created directory and you will notice that everything is inside : the interpreter, other tools , needed libraries and header files

 

In python 3 you can create a virtual environment using the command:

# python -m venv myenv2

 

Activating the environment:

To activate the environment run the activate script in the current shell:

# source bin/activate

On windows you need to run activate.bat script in the Scripts directory

You will see the virtual env name in your prompt:

(myenv) developer@comp1: ~/myenv$

Note that the PATH variable is changed to include the environment bin directory first

 

Installing packages in the virtual environment:

You can install any package or tool in your virtual environment using pip. 

Installing Jupyter and ipython:

# pip install ipython
# pip install jupyter

You can find the tools in the bin (scripts in windows) directory

 

Installing some packages

# pip install numpy 
# pip install tensorflow

You can find the packages in lib/python/site-packages directory

 

Now you can start jupyter notebook and play with numpy and tensorflow

# jupyter notebook

 

Saving the virtual environment:

If you find create many virtual environments with the same packages (different web applications for example) you can save the requirements and install it again easily :

# pip freeze > requirements.txt

Use the file requirements.txt in another environment using a simple command:

# pip install -r requirements.txt

 

Leaving the virtual environment

To leave the environment:

# deactivate

The bin directory is removed from the PATH variable

 

Delete the virtual environment

Simply delete the directory

 

 

 

 

 

 

 

 

 

3 thoughts on “Python – Working With Virtual Environments

  1. Thanks foor this informative read, Ihave shared it on Facebook.

  2. Thank you for sharing wonderful information about that python working with virtual environments.

  3. Welcome everyone ! I’m Alanis Patton.
    Even though I jokingly credit my aunt for my writing talent, I know that it is a skill I have fostered from childhood. Though my mother is a writer, I also started out young.
    I’ve always had a way with words, according to my favorite professor . I was always so excited in English when we had to do a research paper .
    Now, I help current learners achieve the grades that have always come easily to me. It is my way of giving back to schools because I understand the troubles they must overcome to graduate.

    Alanis – Professional Writer – Greaternewarkcharterschool Corp

Comments are closed.