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:

Creating a new environment :

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

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:

 

Activating the environment:

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

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

You will see the virtual env name in your prompt:

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:

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

 

Installing some packages

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

 

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

 

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 :

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

 

Leaving the virtual environment

To leave the environment:

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

Leave a Reply

Your email address will not be published. Required fields are marked *