Mastering Virtual Environments in Python

Before anything else what is a virtual environment? It’s basically a folder structure that gives you everything you need for a lightweight yet isolated Python environment. You can think of it as your own little sandbox to play in without affecting the rest of your system. And let’s be real, who doesn’t love playing in their own sandbox?!

Now, how do we create one? Easy peasy! Just open up your terminal and type:

# This line creates a new Python environment using the venv module
# The -m flag specifies that we are using a module
# The venv module allows us to create isolated Python environments
# The name "my_new_environment" is chosen by the user and can be changed
python3 -m venv my_new_environment

This will generate a new virtual environment called “my_new_environment” in the current directory. You can name it whatever you want, but we recommend keeping it simple and descriptive.

Once your virtual environment is created, activate it by running:

# This line sources the "activate" script from the "bin" directory within the "my_new_environment" directory.
# This activates the virtual environment, allowing you to use the packages and dependencies installed within it.

source ./my_new_environment/bin/activate

On Windows systems, use this command instead:

# This script is used to activate a new environment on Windows systems.

# The following line uses the .\ notation to specify the current directory and then accesses the Activate.ps1 script within the my_new_environment folder.
# This script is used to activate the new environment and set it as the current environment for the user.
.\my_new_environment\Scripts\Activate.ps1

Now you’re in your virtual environment! You can install packages using pip and they will only be available within that specific environment. This is great for managing dependencies between projects or working on multiple versions of the same project simultaneously without conflicts.

But what if you want to customize your virtual environments? Maybe you don’t like the default command prompt name, or you want to overwrite an existing one. No problem! You can do that by running:

bash
# This line creates a virtual environment named "my_new_environment" using the python3 module "venv"
python3 -m venv my_new_environment

# This line activates the virtual environment "my_new_environment"
source my_new_environment/bin/activate

# This line sets the command prompt name to "my_new_environment"
# Note: The "--prompt" flag is not needed as it is already specified in the virtual environment's name
# Also, the virtual environment's name should not be repeated as it will create a nested directory

python3 -m venv my_new_environment

# This line activates the virtual environment "my_new_environment"
source my_new_environment/bin/activate

# This line overwrites an existing virtual environment with the same name
# Note: This may cause conflicts and is not recommended

python3 -m venv --prompt my_new_environment my_new_environment

This will create a new virtual environment called “my_new_environment” with the command prompt name “my_new_environment”. You can replace “my_new_environment” with whatever you want.

You can also skip installing pip and its dependencies by running:

# This line creates a new virtual environment called "my_new_environment" 
# with the command prompt name "my_new_environment". 
# You can replace "my_new_environment" with whatever you want.
python3 -m venv my_new_environment

# This line skips installing pip and its dependencies by using the "--without-pip" flag.
# This can be useful if you already have pip installed or if you want to install it manually later.
python3 -m venv --without-pip my_new_environment

# This line activates the virtual environment, allowing you to use the packages installed within it.
# This is necessary before installing any packages or running any scripts within the virtual environment.
source my_new_environment/bin/activate

# This line installs pip and its dependencies within the virtual environment.
# This is necessary if you skipped installing pip in the previous step.
# Note: The "--no-site-packages" flag is no longer needed as it is the default behavior in newer versions of Python.
python3 -m ensurepip

# This line upgrades pip to the latest version.
# This is recommended to ensure you have the most up-to-date version of pip.
python3 -m pip install --upgrade pip

This will create a new virtual environment without preinstalled packages from the system’s site-packages folder. This can be useful if you want to start with a clean slate or have specific package requirements for your project.

But what about updating Python and its executables? You can do that by running:

# Activate the virtual environment named "my_new_environment"
source my_new_environment/bin/activate

# Upgrade pip, setuptools, and wheel packages using the Python interpreter
python -m pip install --upgrade pip setuptools wheel

This will activate the virtual environment, upgrade pip, setuptools, and wheel to their latest versions, and then deactivate the environment. This is a good practice for keeping your packages up-to-date without affecting other environments or projects on your system.

However, let’s take a step back and talk about how to organize your virtual environments on your system. While creating them in any directory is possible, it can become messy quickly if you have multiple projects with their own set of dependencies.

One way to keep things organized is by using a consistent structure for all your virtual environments. For example:

– Create a folder called “venvs” (or something similar) at the root level of your project or in a common directory on your system, such as Documents or Downloads.
– Inside this folder, create subfolders with descriptive names that correspond to each virtual environment you’re using for a specific project. For instance: “django-venv”, “flask-venv”, and “pandas-venv”.

This way, it will be easier to find the correct virtual environment when working on different projects or managing multiple versions of the same project simultaneously without conflicts. You can also use your preferred IDE’s built-in tools for creating and activating virtual environments in these folders.

Regardless of which approach you choose, remember that virtual environments aren’t entirely self-sufficient Python installations but rely on the base Python’s standard library. This means that you generally don’t put any additional code or information into them manually because anything that goes in there should be handled by your package manager (such as pip). This also means that you shouldn’t commit your virtual environment to version control and you shouldn’t ship it with your project either.

Because virtual environments aren’t entirely self-sufficient Python installations but rely on the base Python’s standard library, you won’t create a portable application by distributing your virtual environment. Instead, consider using tools like Conda or Docker to package and distribute your entire development environment with all its dependencies in one go.

SICORPS