Most Successful Developers use Python Virtual Environments. Do you know how ?
Tags: python, programming
Published: 2021-03-12 07:41:07



*Virtual environments are key to safely tinkering with different versions of Python and combinations of packages*

For some time, Python has included support for managing virtual environments. Python 3.3 even added the built-in **venv** module for creating environments without third-party libraries. Python programmers use several different tools to manage their environments, and the one I use is called [**virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/index.html)**.

Virtual environments are a way of separating your Python project and its dependencies from your system-installed Python. If you use a macOS or Linux-based operating system, it very likely comes with a version of Python as part of the installation, and in fact, it will probably be dependent on that particular version of Python to function properly. But it’s your computer, and you may want to use it for your own purposes. You may need to install another version of Python than the operating system provides. You may need to install some additional libraries, too. Although it’s possible to upgrade your system Python, it’s not recommended. It’s also possible to install other libraries, but you must take care not to interfere with anything the system relies on.

![](https://cdn-images-1.medium.com/max/2048/1*m_5CWeoLKtAI7gEllk_LwQ.png)

For some time, Python has included support for managing virtual environments. Python 3.3 even added the built-in **venv** module for creating environments without third-party libraries. Python programmers use several different tools to manage their environments, and the one I use is called [**virtualenvwrapper](https://virtualenvwrapper.readthedocs.io/en/latest/index.html)**.

Virtual environments are a way of separating your Python project and its dependencies from your system-installed Python. If you use a macOS or Linux-based operating system, it very likely comes with a version of Python as part of the installation, and in fact, it will probably be dependent on that particular version of Python to function properly. But it’s your computer, and you may want to use it for your own purposes. You may need to install another version of Python than the operating system provides. You may need to install some additional libraries, too. Although it’s possible to upgrade your system Python, it’s not recommended. It’s also possible to install other libraries, but you must take care not to interfere with anything the system relies on.

It’s useful to verify your own state of affairs when it comes to the **python** and **pip** commands before starting to use virtual environments. More information about your **pip** instances can be found by running the command **pip debug** or **pip3 debug**.

The equivalent information on my Linux computer, which runs Ubuntu, is almost identical (except that it’s Python 3.8); and it’s very similar on my Macbook, except that the only system Python is 2.6, and I used **brew** to install Python 3.8, so it's located at **/usr/local/bin/python3** instead (along with **pip3**).

![](https://cdn-images-1.medium.com/max/2000/1*ZaPYwaPZkGhrZvF2LDfnnA.png)

**Get the book**: [Learning Python Flask for Beginners](https://leanpub.com/python-flask-for-beginners)

## Installing virtualenvwrapper

You’ll need to install virtualenvwrapper using your system **pip** for Python 3:

    **sudo pip3 install virtualenvwrapper**

The next step is to configure your shell to load the virtualenvwrapper commands. You do this by editing your shell’s RC file (e.g. **.bashrc**, **.bash_profile**, or **.zshrc**) and adding the following lines:

    **export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
    export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
    source /usr/local/bin/virtualenvwrapper.sh**

![](https://cdn-images-1.medium.com/max/2000/0*ZwJ8lEtj4Hzv_chI.png)

If your Python 3 is located elsewhere, change the first line according to your setup.

Close your terminal and reopen it for this to take effect. The first time you open the terminal, you should see some output from virtualenvwrapper. This will only happen once, as some directories are created as part of the setup.

Now you should be able to type the command **mkvirtualenv --version** to verify that virtualenvwrapper is installed.

## Creating a new virtual environment

Say you’re working on a project called **flashylights**. To create a virtual environment with this name, run the command:

    **mkvirtualenv flashylights**

The environment has been created and activated, so you’ll see that **(flashlylights)** appears before your prompt:

![](https://cdn-images-1.medium.com/max/2000/0*8RZAiLNHGH0uhTXJ.png)

Now that the environment is activated, things have changed. The **python** now points at a completely different Python instance than the one(s) you identified on your system earlier. It's created a directory for your environment and placed a copy of the Python 3 binary, the pip command, and more inside it. Type **which python** and **which pip** to see where they're located:

![](https://cdn-images-1.medium.com/max/2000/0*xjZkNPAqY7kl6IMg.png)

If you run a Python program now, you can run it with **python** instead of **python3**, and you can use **pip** instead of **pip3**. Any packages you install using **pip** will be installed within this environment alone, and they will not interfere with your other projects, other environments, or your system installation.

To deactivate the environment, run the command **deactivate**. To re-enable it, run **workon flashylights**.

You can list all available environments with **workon** or use **lsvirtualenv**. You can delete an environment with **rmvirtualenv flashylights**.

Adding virtual environments to your development routine is a sensible thing to do. In my experience, it keeps me from installing libraries I’m experimenting with system-wide, which can lead to problems. I find virtualenvwrapper the easiest way for me to get into that routine and manage my project environments hassle-free without thinking too much or remembering too many commands.

## Advanced features

* You can install multiple Python versions on your system (e.g., using the [deadsnakes PPA](https://tooling.bennuttall.com/deadsnakes/) on Ubuntu) and create a virtual environment with that particular version using, for example, **mkvirtualenv -p /usr/bin/python3.9 myproject**.

* You can automate activation/deactivation upon entering/leaving a directory.

* You can use the **postmkvirtualenv** hook to install common tools every time a new environment is created.

**Recommended resources to learn Python:**

[Web Developer Bootcamp with Flask and Python](https://click.linksynergy.com/deeplink?id=NcMZU1aTATA&mid=39197&murl=https%3A%2F%2Fwww.udemy.com%2Fcourse%2Fweb-developer-bootcamp-flask-python%2F)

[Learn Python Programming Masterclass](https://coursesim.com/course/learn-python-programming-masterclass/)

Reference sites: [https://opensource.com/article/21/2/python-virtualenvwrapper](https://opensource.com/article/21/2/python-virtualenvwrapper)

---

Other interesting posts:

- [Making your Python Debugging Sweeter with some “IceCream” 🍦😉](https://dev.to/bricourse/making-your-python-debugging-sweeter-with-some-icecream-87i)
- [Making your App really Sexy with a React Singleton Hook](https://dev.to/bricourse/making-your-app-really-sexy-with-a-react-singleton-hook-2f9n)
- [Finally Understanding Javascript Closures like the Pros](https://dev.to/bricourse/finally-understanding-javascript-closures-like-the-pros-3pgd)