Virtual Environments for Python

Martin Breuss
4 min readMay 31, 2018

--

With a bustling activity as a developer, when grinding projects feels like a morning jog, it is easy for something to go out of hand: dependencies.

What are dependencies?

When building python projects and with the rich ecosystem of modules and packages out there, you’ll want and use all that code. Dependencies are python packages containing code that someone else wrote and that you are utilizing for your project. The developers who wrote the code put a lot of effort into making the wheels they built easy to use. Exactly: so that you won’t have to re-invent them.

The challenge

Re-using code is a great thing! However, fast-forward into maintaining three different Django projects for different clients who are all using different versions of Django and life starts looking less like a paved road. Your system can only have one version of Django installed, so you’ll quickly run into troubles. The examples of such issues with dependencies are manyfold.

The solution: virtual environments

Maybe you’ve heard about virtual machines before. A virtual machine allows you to run e.g. Linux on your Mac operating system. Programs such as virtualbox help you to create a separate environment that is self-sufficient and doesn’t need to interact with the main OS.

Virtual environments for python behave similarly, only that they don’t box up a whole OS, but instead:

  • a full python installation (including pip)
  • whichever modules and packages you add

Using virtual environments makes software development safer and allows you to keep your handsome hairline intact years longer into the future. Many beginner courses don’t introduce the concept of virtual environments until much later, but learning about them and using them from the get-go can be a huge advantage and time-saver. And it’s pretty easy! So here’s a quick guide for creating our first virtual environment.

Creating a virtual environment

python 3 makes creating virtual environments (venvs) a breeze. Here’s a quick look at the syntax:

python3 -m venv your_venv_name

Easy, right?

By default we’ll call our venv’s simply env - but if you want to go wild with naming them, go ahead. Just remember what you called it!

Navigate to the folder that was created by this command. Check out which files you can find in /bin. Every freshly minted venv comes with a shiny new version of python as well as pip preinstalled.

Using a virtual environment

Fine, so all of this env stuff now exists — but behold! You are not yet inside the safe space! In order to enter your new venv you’ll have to source it by typing the magic command:

source env/bin/activate

All that this does is running the activate script which grants you access to the safe kingdom of venv.

Mufasa: “already today all these pixels are yours”

After the command has executed you’ll see an indicator that you are now inside of the virtual environment through having the name you gave the venv in brackets in front of each terminal command.

For me this looks like so:

Depending on the shell you are using it might look a little different (e.g. less colourful) — but the concept is the same.

Seeing your virtual environment’s name in brackets means you can now start breathing again. You are in a safe place. ;)

And… how can I get out of here?

To return to your normal system’s environment you only need to say one word to the console:

deactivate

The bracket-part will disappear and your shell will show your normal prompt again. That’s it. Welcome home. 🏡

Quick task: Create a venv and install Django

  1. using the terminal create a new folder called testenv
  2. navigate into the folder
  3. create a venv called env inside of the foldertestenv
  4. activate env
  5. use pip to install Django
  6. open the python interpreter and import django to verify that it all worked

🎉

Note: Make sure that your virtual environment’s path doesn’t include any whitespace. Otherwise the executables won’t be found and you might end up accidentally using the system-wide install of pip. See: https://stackoverflow.com/a/7911058/5717580 for more information.

If you want to learn more about Web Development with python and Django (and everything that comes with it) feel free to contact me about the python bootcamp I’ll be co-teaching this summer in Barcelona with CodingNomads! Maybe you’ll look for your private safe little kingdom in Europe on the way ; )

--

--