Mastering Python Development Environments: A Comprehensive Guide to Virtual Environments
jaikishpai
Posted on December 27, 2023
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`
- To activate the virtual environment:
path\to\myenv\Scripts\activate
- Deactivate the virtual environment using the following command:
deactivate
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
- Activate the virtual environment using the following command:
conda activate myenv
Use conda
to install, update and manage packages. Deactivate the virtual environment using the following command:
conda deactivate
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 thepip
command
pip install pipenv
- Navigate to the project directory
- Create a virtual environment using the below command.
pipenv install
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
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
- Deactivate the
pipenv
virtual environment using the command:
exit
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.
Posted on December 27, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
December 27, 2023