Python Environment and Package Management

2024-07-14

Python Environment Thumbnail Image

When I first started learning Python, one of the things that confused me as a beginner was understanding the environment where Python is used and developed. There were so many terms—libraries, modules, packages, interpreters—and unfamiliar concepts like pip, pyenv, pipenv, venv, virtualenv, etc. Their usage can often overlap, making it almost certain that beginners will feel confused.

When learning Python, it’s important to understand the functions and differences between environments and packages. In this series, we will cover three key topics that are commonly encountered when working with Python: version management, package management, and environment management.

Python Packages Image

Version, Package, and Environment?

Version

Version refers to a specific release of Python or a package/module in Python. Versions are typically denoted by a series of numbers separated by periods, such as “3.12.4” or “2.7.18”. You can see the full Python release versions on the official Python version documentation page.

Package

A package refers to a collection of Python modules bundled together to provide a complete functionality. Packages can be libraries, frameworks, or tools. The purpose of a package is to make it easier for developers to reuse code across different projects without having to rewrite it from scratch. Think of a package as a toolbox where you can store various work tools (functions and classes), which can be easily accessed and used across different projects. Examples of Python packages include frameworks like Flask, Django, FastAPI, Pyramid, or libraries commonly used in data science, such as NumPy, Pandas, and Matplotlib.

Environment

Python is not very dependency-friendly, which can cause difficulties when working on multiple projects that require different dependencies. This can lead to conflicts and issues. Imagine working on two Django projects: one project continues using version 3.1.14, while the other is a new project using the latest version of Django, 5.0.7. If Django is installed globally, the last installation will overwrite the previous one, so only the most recently installed version will function. This is where virtual environments come into play. Virtual environments are used to accommodate different projects with frameworks that require different versions without relying on the globally installed version.

In Python, a virtual environment can be compared to an isolated workspace where you can manage project dependencies without affecting other projects.

Interpreter

The interpreter is a computer program responsible for translating source code in a high-level programming language into intermediate-level code or machine-readable code. The interpreter runs and translates all the code input into the program.

Python offers many tools to manage versions, packages, and environments. Some tools serve a single purpose, while others are multi-purpose. The following matrix summarizes some commonly used tools:

Python Environment Matrix Image

1. Python Version Management

Python version management refers to the practice of installing, maintaining, and switching between different versions of the Python interpreter on a system. This practice ensures compatibility across projects that may require different versions of Python.

Python Version Management Image

One common tool used to manage Python interpreter versions is pyenv. This tool is ideal for developers who need to easily switch between different Python versions. pyenv has one function: to manage Python versions.

pyenv can set the Python version globally, locally, or for the shell.

1. Global: A specific version of Python installed globally or as the default Python. This version will be used if no other specific Python version is installed.

    pyenv global <python_version>
    example:
    pyenv global 3.9.7

2. Local: A specific Python version installed in a project directory and its subdirectories. This version will override the global version when inside the local directory.

    pyenv local <python_version>
    example:
    pyenv local 3.9.7

3. Shell: A specific Python version installed for the duration of a single terminal session. This version is temporary and reverts back to the local or global version when the terminal is closed or reopened.

    pyenv shell <python_version>
    example:
    pyenv shell 3.9.7

2. (Virtual) Environment Management

Environment management is a method of managing the development environment for a Python project. In practice, a virtual environment in Python is a folder containing the necessary packages and dependencies for a Python project. This virtual environment isolates dependencies and packages to prevent conflicts with other projects using different versions and packages.

Python Environment Management Image

Several tools can create virtual environments. Some tools, like venv and virtualenv, are used only for creating or managing virtual environments. Other tools, like pipenv, offer multi-purpose functionality.

virtualenv

virtualenv is an external package that needs to be installed separately. It can be used with both Python 2 and 3. virtualenv is useful if additional features and customization options are needed, or if integration with third-party tools is required.

To install virtualenv:

pip install virtualenv

To activate a virtual environment:

	•	Windows: myenv\Scripts\activate
	•	MacOS: myenv/bin/activate

venv

venv is part of the standard library, integrated into Python versions 3.3 and above. Unlike virtualenv, venv does not need to be installed, but it only works with Python 3. venv is typically used to isolate basic project dependencies without additional features.

To use venv:
python3 -m venv <env_name>
. <env_name>/bin/activate
deactivate

pipenv

pipenv is a tool for managing both virtual environments and packages. It was designed to simplify the process of creating virtual environments compared to traditional methods using pip and virtualenv. pipenv combines the functionality of pip and virtualenv into one tool, ensuring more consistent and secure dependency management.

To install and activate pipenv:

pip install pipenv
pipenv shell

pipenv creates two directories: Pipfile and Pipfile.lock. Pipfile contains the specific dependencies for the project, while Pipfile.lock ensures that the project uses locked versions of packages to prevent dependency conflicts.

3. Package Management

Package management is an important aspect of software development, involving the installation, updating, configuration, and removal of packages. The tool commonly used for package management in Python is pip.

pip stands for “pip installs packages”. It is a tool used to install and manage packages in Python. To install a third-party package with pip:

pip install package_name

Python Package Management Image

In the context of package management, installed packages depend on the Python configuration and version:

• pip: The standard command used to install Python packages. • pip2: Used to install packages specifically for Python 2.x versions.

    pip2 install package_name

• pip3: Used to explicitly install packages for Python 3.x versions.

    pip3 install package_name

References:

• An unbiased evaluation of environment management and packaging tools by Anna-Lena Popkes


The beauty of programming is that there's always something new to learn. The learning process is never-ending, and that's what keeps it exciting. This is my ongoing journey into the world of programming. I'm constantly learning and growing, and I'm excited to share my experiences with you as I progress.

const developerName = "Ano Jumisa"