Python Virtual Environments

September 28, 2024 - 3 min read

TLDR

python -m venv myenv (Creates myenv)
source myenv/bin/activate (Activates myenv)
deactivate (Deactivates myenv)

The Actual Explanation

When installing and dealing with Python programs, virtual environments are a commonplace practice to avoid versioning errors. If you have no idea what that is, versioning errors are when an application or script reqiures a certain version of a tool to function, but finds a different version installed. Sometimes this results in a function of the tool not being available, or acting in a different manner than expected, producing a versioning error.

A Python virtual environment is an isolated workspace that allows you to manage dependencies for different projects independently. To create one, first ensure that Python is installed on your system. Open a terminal and navigate to your project directory. Run the following command to create a virtual environment:
python -m venv myenv

This will generate a new folder called myenv, containing the necessary files for the environment. To activate it, use:

Once activated, you can install packages using pip, and they will be isolated from the system-wide Python installation. To deactivate the environment, simply type deactivate.

Virtual environments help avoid version conflicts between dependencies, ensuring your projects run in consistent environments across machines. To delete an environment, just remove the myenv folder.

For a better and more technical explanation of Python Virtual Environments, visit the Python docs here.