Virtual Environments in Python Applications

Virtual environments are a vital part of modern application development, regardless of the stack you are using. Virtual environments avoid problems related to dependency and version incompatibility. In this guide, I will show you three popular ways to use virtual environments in Python.


Venv

Python Venv is a built-in tool to create and manage lightweight virtual environments.

For more information visit: venv tutorial.

Installation:

Already installed on MacOS and Windows platforms, but needs to be installed on some Linux distros, here is an installation guide for different package managers:

sudo apt install python3-env # using apt
sudo dnf install python3-env # using dnf
sudo pacman -S python3-env # using pacman
Enter fullscreen mode
Exit fullscreen mode

Creating a Virtual Environment:

python -m venv <env_name> # Unix-like systems
python -m venv <env_name> # windows
Enter fullscreen mode
Exit fullscreen mode

Activating the Virtual Environment:

  • On Unix-like systems (Unix, Linux, or MacOS):
source <env_name>/bin/activate
Enter fullscreen mode
Exit fullscreen mode
  • On Windows:
<env_name>\Scripts\activate
Enter fullscreen mode
Exit fullscreen mode

Deactivating the Virtual Environment:

deactivate
Enter fullscreen mode
Exit fullscreen mode

Virtualenv

virtualenv is a simple to use tool to create isolated Python environments.

For more information visit: virtualenv documentation.

Installation:

pip install virtualenv
Enter fullscreen mode
Exit fullscreen mode

Creating a Virtual Environment:

virtualenv <env_name>
Enter fullscreen mode
Exit fullscreen mode

Activating the Virtual Environment:

  • On Unix-like systems (Unix, Linux, or MacOS):
source <env_name>/bin/activate
Enter fullscreen mode
Exit fullscreen mode
  • On Windows:
<env_name>\Scripts\activate
Enter fullscreen mode
Exit fullscreen mode

Deactivating the Virtual Environment:

deactivate
Enter fullscreen mode
Exit fullscreen mode

Pyenv (Unix, Linux or MacOS only)

Pyenv is a popular option in Python applications. Pyenv allows you to manage different versions of Python in your application and also on your system.

Pyenv does not officially support Windows but you can use WSL (Windows Subsystem for Linux).

For more information visit: pyenv repository

Installation:

You can install using Homebrew or make a manual installation:

Creating a Virtual Environment:

pyenv virtualenv <python_version> <env_name>
Enter fullscreen mode
Exit fullscreen mode

Activating the Virtual Environment:

pyenv activate <env_name>
Enter fullscreen mode
Exit fullscreen mode

Deactivating the Virtual Environment:

pyenv deactivate
Enter fullscreen mode
Exit fullscreen mode

Managing Python Versions:

Pyenv allows you to manage python versions, here is a simple tutorial that you can use in your application or in your input system:

  • List available versions:
pyenv install -list
Enter fullscreen mode
Exit fullscreen mode
  • Install a version:
pyenv install <version>
Enter fullscreen mode
Exit fullscreen mode
  • Defining a version:
pyenv global <version> # set a global version
pyenv local <version> # set a local version
Enter fullscreen mode
Exit fullscreen mode

Conclusion:

The tools are very similar, the biggest difference is in the installation process and in some specific features, such as python version management (pyenv), you can choose according to your taste or personal preference, I recommend that you test each one of them and see that best suits your project.


Discover more from Coursity

Subscribe to get the latest posts sent to your email.

Leave a Comment

Your email address will not be published. Required fields are marked *

Discover more from Coursity

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top
Chinese, continental, indian foods.