Skip to content

Instantly share code, notes, and snippets.

@ashwin-nair-gh
Created September 10, 2020 00:48
Show Gist options
  • Save ashwin-nair-gh/72673136b5cc278b47867079949f59ca to your computer and use it in GitHub Desktop.
Save ashwin-nair-gh/72673136b5cc278b47867079949f59ca to your computer and use it in GitHub Desktop.

Exploring Virtual Environments in Python - Part 1

This is part one on virtual environments in Python.

Do you want to create a virtual environment? Or perhaps you already created one, and found yourself stalled after making it? Don't worry! This article will address why we need virtual environments and how to create them.

Virtual environments[1] allow you to have a completely isolated environment for your project. This means that the dependencies for your projects will not interfere with the dependencies of other projects. Python standard library has a module called venv[2], which was first introduced in Python 3.3, just for this purpose. Let's try to make a virtual environment with venv. But first, I suggest going to your file directory and making a few new folders. See below:

fileDir

This folder structure will allow us to test the virtual environment system simply. test.py contains a simple print statement that says "test.py". test2.py says "test2.py". Pretty simple, right?

Now let's move on to creating the virtual environment. Go to your terminal application, and type the following line:

https://gist.github.com/5925c6899160698f87e1d3894f1a403c

Make sure you've correctly typed everything; typos matter. For example, if you write /Project3/ instead of /Project1/, Python will create a new directory called Project3, and create a virtual environment within it. Now, let's check the Project1 folder, and you should see 3 new folders, a new config file, along with our previously made test.py file.

fileDir2

If your directory looks like mine, congratulations! You've made your first virtual environment!

The Project folders were created to show two separate projects, both of which will have two separate virtual environments. The pyvenv.cfg file is a file that stores some metadata such as the version of the Python interpreter. Now to start working within the environment, you have to activate it. This is done from the terminal. Open your terminal application once more and type the following lines:

https://gist.github.com/23e0636ee1d071fee01d605e3283e28a

Now, you're in the project directory. Next, let's activate the virtual environment.

https://gist.github.com/05da7bed644c0c81cb00de2af7d52e09

If this worked, you should notice an immediate change to your terminal. Your terminal should've changed from:

https://gist.github.com/d52c3bccfcf6cdfd061822c1373e9db9

to

https://gist.github.com/d721588ad36e1fd9f1416c15a6e61ee2

Adding the name of the virtual environment's name at the beginning of the prompt is Python's way of telling you that you're in an active virtual environment. Please note that the percent sign at the end of my prompt signifies where the user input starts. Your terminal may use another symbol, but they all have the same basic meaning.

Let's try installing a package using pip. Pip[3] (preferred installer program) is the preferred program installer. From Python 3.4 onwards, venv automatically installs pip into the virtual environment. Keep in mind that if you're using Python 3 like me, you should do pip3 install[4] instead of pip install, to specify which Python installation you want to use. To uninstall a package you've installed, you can use pip3 uninstall [package name][5] to uninstall it. For this example, I chose the Pandas[6] library, a very popular data analysis module. First, let's deactivate our virtual environment, simply by typing deactivate:

https://gist.github.com/c3f218c0f7906a7dcc55133666621868

Now, the (Project1) text will disappear from the prompt, and this signifies that we are no longer in the virtual environment. Now let's try installing pandas:

https://gist.github.com/6afe8a5a8c49d6b08951047803e1df36

Since we're not currently in a virtual environment, installing pandas will install it to the system library, and not into our virtual environment. If you have pandas installed, you'll see a different screen, which shows pandas being downloaded. In my case, you can see that I already have pandas installed. If so, try pip3 install pandas again, and you should see the screen above. Now we've established that pandas are downloaded. Let's activate the virtual environment and try installing pandas again:

https://gist.github.com/6e5a5a177f7ccb9bad860b7883ef226f

What does this mean? This signifies that our virtual environment is isolated and working; no changes in the virtual environment will affect the rest of the system and vice versa. This is very important since now our virtual environment will work independently of what's happening outside of it; even if we were to update to a new version of Python on a global scale, we would be using the same version within the Virtual Environment.

Now say you want to transfer your installed packages into another virtual environment. This can be simply done by using a requirement file[7], created by using the pip freeze[8] command. To demonstrate this, we'll make use of our Project2 directory, and make a virtual environment inside it. First, let's create the requirements file.

All you have to do is type:

https://gist.github.com/4922eff1e902a9d081fa41e33f56675e

This will record all the installed packages and their respective dependencies from Project1 into the newly created requirements.txt file. Now, let's go to the Project2 directory, and create a new virtual environment:

https://gist.github.com/9dd4769c4409ab6551c6def342966a7c

Now, copy and paste the requirements.txt file into the Project2 directory. Next, activate the virtual environment within the Project2 directory, and install from the requirements file:

https://gist.github.com/eca87a0cda8bca8bf8a22136f20bc357

To see all installed packages, try pip3 list[9]. The output will look something like this:

https://gist.github.com/cd47590878883f624d446508403b779d

To get more information on one of these installed packages, you can try pip3 show[10]. It's very helpful in understanding what each package is used for since packages can quickly build up when working on a project.

https://gist.github.com/91ec42af8586e48fea86777c40929094

To get a specific version of a particular package, you can use pip3 install numpy==1.19.1; This will install version 1.19.1 of the numpy module:

https://gist.github.com/51a51a5e00ad02530f68a56734348786

You can also search for modules right from your terminal, using pip search [topic][11]. See below:

https://gist.github.com/24ea2cb705caa4efe455b8c46a311eff

In closing, this article showed you the basics of a virtual environment and some of the pip commands that are often used to customize them. We learned how to install, search for, export, and find documentation on Python packages, all with pip. I hope that this article helped you set up your virtual environment; keep your eyes open for the next article!


References

  1. Virtual Environments & Packages - Python 3 Docs

  2. venv - Python 3 Docs

  3. Pip - Python Docs

  4. Pip Install - Pip Documentation

  5. Pip Uninstall - Pip Documentation

  6. pandas - Pandas Docs

  7. Requirement files - Pip User Guide

  8. Pip Freeze - Pip Documentation

  9. Pip List - Pip Documentation

  10. Pip Show - Pip Documentation

  11. Pip Search - Pip Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment