DEV Community

Cover image for Mastering Python Development Environments: A Comprehensive Guide to Virtual Environments
jaikishpai
jaikishpai

Posted on • Updated on

Mastering Python Development Environments: A Comprehensive Guide to Virtual Environments

Python has been my go-to programming language since I started coding. Python, as a programming language, needs little introduction. Virtual environments in Python are essential for isolating project dependencies, ensuring compatibility, and maintaining clean, reproducible development environments. They allow developers to manage specific package versions for each project, avoiding conflicts and facilitating collaboration by providing encapsulated spaces for code development.

There are several ways to create a virtual environment for a Python project, and I would like to share some thoughts on three different methods that I have explored to date:

venv- It is a Python module (3.3 and higher). This module allows the creation of lightweight "virtual environments," each with its own Python packages. Based on an existing Python installation, a virtual environment creates a "base" Python environment whose packages are isolated from those in the base environment, making the virtual environment available only to those explicitly installed in it.
Steps to create a virtual environment using venv:

  • Create a virtual environment using the following command with the name myenv:
python -m venv path\to\myenv`
Enter fullscreen mode Exit fullscreen mode
  • To activate the virtual environment:
path\to\myenv\Scripts\activate
Enter fullscreen mode Exit fullscreen mode
  • Deactivate the virtual environment using the following command:
deactivate
Enter fullscreen mode Exit fullscreen mode

Conda- It is an open-source package management system for Windows, macOS, and Linux. Developed by Anaconda, Inc., Conda simplifies installing, managing, and updating packages and environments.
Conda is particularly popular in the data science community, but I have been using it for software development purposes.

Steps to create a virtual environment using conda

  • Install Anaconda or Miniconda on the machine.
  • Run the below command. myenv is the virtual environment name.
conda create --name myenv
Enter fullscreen mode Exit fullscreen mode
  • Activate the virtual environment using the following command:
conda activate myenv
Enter fullscreen mode Exit fullscreen mode

Use conda to install, update and manage packages. Deactivate the virtual environment using the following command:

conda deactivate
Enter fullscreen mode Exit fullscreen mode

pipenv-It brings together the most desirable features of other packaging tools, such as pip, virtualenv, and requirements.txt, into a single, cohesive tool. Pipenv uses a Pipfile to manage project dependencies, allowing deterministic builds and simplified dependency tracking. It is easy to use and provides a consistent experience when working with Python packages. Pipenv integrates package installation and virtual environment creation into a single command, providing a seamless and user-friendly experience.

Steps to create a virtual environment using pipenv

  • Install pipenv using the pip command
pip install pipenv
Enter fullscreen mode Exit fullscreen mode
  • Navigate to the project directory
  • Create a virtual environment using the below command.
pipenv install
Enter fullscreen mode Exit fullscreen mode

This command create a virtual environment with the name of the project and installs the projects dependencies based on the Pipfile

  • Activate the virtual environment using the following command:
pipenv shell
Enter fullscreen mode Exit fullscreen mode

To install packages for the project use the following command which will install the package and also add the package to Pipfile:

pipenv install package_name
Enter fullscreen mode Exit fullscreen mode
  • Deactivate the pipenv virtual environment using the command:
exit
Enter fullscreen mode Exit fullscreen mode

My experience using Conda was extensive until I came across Pipenv, which seemed like it would be a good tool for managing packages. Pipenv streamlines the workflow by integrating the creation of virtual environments and package management into one tool.

I would be interested in learning about other tools for managing virtual environments.

If you enjoyed my content and would like to show support, you have the option to Buy Me a Coffee. Your contribution would serve as encouragement for me to create more similar stories in the future.

Top comments (0)