DEV Community

loading...
Cover image for Most Successful Developers use Python Virtual Environments. Do you know how ?

Most Successful Developers use Python Virtual Environments. Do you know how ?

bricourse profile image bricourse ・5 min read

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**.

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.

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**.

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).

Get the book: Learning Python Flask for Beginners

Installing virtualenvwrapper

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

**sudo pip3 install virtualenvwrapper**
Enter fullscreen mode Exit fullscreen mode

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**
Enter fullscreen mode Exit fullscreen mode

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**
Enter fullscreen mode Exit fullscreen mode

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

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:

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 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

Learn Python Programming Masterclass

Reference sites: https://opensource.com/article/21/2/python-virtualenvwrapper


Other interesting posts:

Discussion (0)

pic
Editor guide