DEV Community

Cover image for Virtual Environments in Python Applications
Wesley Bertipaglia
Wesley Bertipaglia

Posted on

2 1 1 1 2

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
    
  • On Windows:

    <env_name>\Scripts\activate
    

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
    
  • On Windows:

    <env_name>\Scripts\activate
    

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:

  • Using Homebrew:

    brew install pyenv
    
  • Manual Installation:

    • Download pyenv: bash curl https://pyenv.run | bash
    • Let's edit your bash profile: bash nano .bashrc
    • Add these lines in the end of the document: bash export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)"

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
    
  • Install a version:

    pyenv install <version>
    
  • Defining a version:

    pyenv global <version> # set a global version
    pyenv local <version> # set a local version
    

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.

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay