Python Virtual Environments

Put your code in a Greenhouse

Inês Pessoa
Analytics Vidhya
4 min readJun 10, 2020

--

Photo by www.zanda. photography on Unsplash

A python environment can be compared to a greenhouse.

If you switch a plant from your yard to a proper greenhouse, the genome of the plant will not change, but its behavior will. It will grow in optimal conditions, with no interference from the current weather.

A plant in a greenhouse is not affected by external weather conditions.

With a python project is the same. If we run the project inside an environment, we are only using the dependencies needed, and its behavior will not change with time. If we use the default python environment for every project, we may end up deleting or updating dependencies that may affect the behavior of some of those projects. Also, by using a python environment dedicated to a project, it gets easier to set up the project in other machines.

A project in a dedicated and isolated environment is not affected by changes in the default environment.

With this said, let’s avoid damages caused by storms, and start isolating our code with python environments.

To do this, we will use virtualenv to create and manage an environment called greenhouse.

To install virtualenv manager on your machine, type the following instruction on your command line:

Note: The bash commands below are compatible with Linux and MacOS.

$ python -m pip install --user virtualenv

Then, by the time you have virtualenv installed with success, you can start creating python virtual environments. To create our greenhouse environment, we need to write the following command:

$ python -m venv greenhouse

The environment will be created in the directory where you type the command.

If you want to start working inside your environment, from your command line, you need to activate it.

$ source greenhouse/bin/activate

Be aware that you are only working on your greenhouse environment if you see the environment name as a prefix of your command, like the following:

(greenhouse) $ <your command here>

If you want to check the location where your current environment is, write the following on the terminal:

(greenhouse) $ which python 

Now you can start installing dependencies and/or running your python code inside this environment and work with it from the command line.

To install dependencies on this environment, do the following:

(greenhouse) $ pip install <package name>

After installing all the dependencies you need for the project in your dedicated environment, you can generate a requirements file. This file lists all the dependencies that are needed to run the project.

(greenhouse) $ pip freeze > requirements.txt

A requirements.txt file was created on the current directory. Now you can share this file along with your project so others can run your code on their machines easily.

On the other side, if a code is shared with you, you can also create a new environment dedicated to that project, and install all needed dependencies from a requirements file, by doing:

(greenhouse) $ pip install -r requirements.txt

When you stop working on your project that uses the greenhouse environment, you should deactivate it. To do this, type the following command:

(greenhouse) $ deactivate

Last but not least, we learned how to activate the environment in the command line, but in the development phase, we need to activate it on an IDE.

As an example, if you are working with pycharm, on the top bar, select File and then Settings. Go to project, and select Project Interpreter.

Select the icon at left. A new window, like the following, will be opened.

On that window, search for the location where you stored the virtual environment, and select the python file, inside the bin folder.

Click ok, and then apply the new settings. With this done, the greenhouse environment is now active on the current pycharm project.

If you are working with Jupyter Notebook, in order to work on notebooks using your environment, you can launch jupyter notebook from the command line with your environment activated.

(greenhouse) $ jupyter-notebook

If you open a notebook, now you are working inside the greenhouse environment.

To sum up, in this article you learned why environments should be used, how to create and manage python environments from the command line and how to activate it on pycharm and jupyter notebook, so you can have it activated also when you are developing your code.

I hope you enjoyed this content. See you at the next learning adventure.

--

--