Understanding Python Virtual Environments

If not, here is an example:Photo by Hammer & Tusk on UnsplashRecall the definition of an environment for a human — their surroundings.

For the person in the above photo, their surrounding is what he is seeing through the lens.

For him, the notion of reality (to an extent) has changed from what he was seeing through his naked eyes to what he perceives now.

A virtual environment for python is a similar idea: you give it a separate “environment” inside an environment (your computer) and allow it to operate there.

However, there are a few key differences between a VR experience for a human and the VR experience for python.

Firstly, you can create multiple virtual environments for python (you can consider that python has an infinite number of faces, as compared to just 1 for humans so python can wear as many VR glasses it wants).

Secondly, while a human can still touch and feel objects from the “real” environment, python may not necessarily do so.

When you put on VR glasses for python, it may or may not have access to the “real” environment (depending upon your settings which are a bit too advanced for this post).

As such, all the libraries and packages installed on your computer (aka the real environment) are inaccessible in a virtual environment and vice-versa.

Why should I care?Ok, this sounds cool but what is the purpose of a virtual environment?.Why give cool VR glasses to boring and complex programming languages?Convenience.

Stability.

Ease of mind.

Not for the programming language, but for you as the programmer.

Notice how different projects may require different versions of libraries and maybe even languages?.That’s where virtual environments are useful.

The idea is to create a separate virtual environment for each project.

Since the resources (libraries, packages, etc,) of a language are hidden from the real environment and other virtual environments, there is no interference and no clashes between versions.

For example, consider that you update the library X in the virtual environment for project A.

Since you updated it inside the virtual environment for project A, you can be sure that this doesn’t cause the library X to update in any other environments.

Therefore, your code and its dependencies in other places are unaffected.

OK, how do I make a virtual environment?For this story, I’ll be covering the virtualenv library, but there are many different (and maybe better) ways to create python virtual environments.

I’ll also be using a Mac, which is a UNIX based system.

Hence, if your computer uses a different system, the steps might be varied.

You might not have virtualenv installed on your system.

You can check the same using:virtualenv –versionIf you have it installed, terminal will print the version (e.

g.

16.

4.

3) of virtualenv installed on your system.

Else, it should give you a “command not found” error.

If that is the case, enter the following command:pip install virtualenvPS: if you don’t have pip, you can download it for your system.

It is included by default with binary installers for Python 3.

4 or higher.

Next up, navigate to a directory where you want to create a virtual environment.

You can create another directory just for environments and change to that directory with the following command:mkdir Environments && cd EnvironmentsThis creates a new folder called “Environments” and opens the directory in the terminal.

You’re now ready to create VR experiences for python.

To create an environment, use the following commandvirtualenv project_name The above command creates an environment called “project_name” inside your current working directory.

Replace the “project_name” with your project’s name.

After completing the above steps, your terminal window should look something like this:Terminal screen after creating the virtual environment.

However, the virtual environment is not activated yet.

Every time you want to go inside the environment or to activate it (aka wear the glasses), you need to run the following command:source project_name/bin/activateTada!.You’re in your virtual environment now.

How do you confirm that you are indeed inside a virtual environment?.Your terminal will prefix the project_name in all the lines as shown below:Terminal screen after activating python virtual environmentThat’s it!.You’ve successfully learnt, created and activated a python virtual environment.

You can now work on your project inside this virtual environment.

You can install libraries as you would on your normal terminal, except that these will be installed in the virtual environment and not the “real” environment.

You can exit the virtual environment using the following command to “log-off” the virtual environment:deactivateResources for further learning:You haven’t discovered the world of virtual environments entirely yet.

There are many interesting things you might want to explore:This story compares different python virtual environment tools (recall that I only showed virtualenv):Comparing Python Virtual Environment toolsVarious tools to set up Python Virtual Environmentstowardsdatascience.

comThis is a great article for being a pro at virtual environments:How to manage multiple Python versions and virtual environments ????Addition January 2019: If you are coming back to this blog after upgrading to macOS Mojave please see this github issue…medium.

freecodecamp.

orgThe following article discusses the latest developments in relation to virtual environments:Goodbye Virtual Environments?If you’re a Python developer you’ve likely heard of virtual environments — “a self-contained directory tree that…medium.

comI hope this article helps you understand what virtual environments are and gets you started with them.

If you have questions, please do not hesitate to respond to this story or contact me.

Happy VR-ing with Python!.

. More details

Leave a Reply