Virtual Environment and Python package manager

What is Virtual Enviroment?

  • Virtual Environment in Python is an isolated space where you can work on your Python projects, separately from your system-installed Python. You can set up your own libraries and dependencies without affecting the system Python. We will use virtualenv to create a virtual environment in Python.

  • Let’s Consider Inventory Management, Accounting System and HR System to show Python Virtual Environment concept

System 1 : Inventory Management

  • In an Inventory Management System (IMS) developed in Python, the concept of a virtual environment is crucial for managing project-specific dependencies, ensuring reproducibility, and avoiding conflicts between different versions of libraries.

System 2 : Accounting System

  • An accounting system developed in Python can use a virtual environment to manage its dependencies, ensuring that the system is portable, isolated, and compatible across different platforms. Like in other systems, the virtual environment helps keep project-specific libraries, ensuring the system behaves predictably without version conflicts.

System 3 : HR System

  • In a Human Resources (HR) system built using Python, the concept of a virtual environment is key to managing dependencies and ensuring a smooth and isolated environment for the system to run. This setup avoids conflicts between libraries and ensures that the HR system behaves predictably across different machines or environments.

Why Virtual Enviroment is Important?

  1. Dependency Management - Virtual environments allow you to isolate these dependencies, preventing conflicts between projects. Reproducible Environments: By creating a virtual environment and specifying the required packages, you can ensure a consistent and reproducible environment for your project, regardless of the host system.

  2. Isolation - Virtual environments are self-contained, so you can have different projects using different Python versions and libraries without conflicts.

  3. Cleaner Development Workflow - With virtual environments, your development environment remains clean and uncluttered. You won't accidentally install packages globally, which could lead to conflicts or difficulties in tracking dependencies.

How to Use Virtual Environments?

  • This is step-by-step guide on how to use and create virtual environment.

Create a Virtual Environment

  • First, before you create a python virtual environment create first a folder where you will save your python environment using this command in your command prompt.
mkdir foldername
  • Second, go to that folder using this command
cd foldername
  • To list the folder or file use this command
dir
  • Last create a python virtual environment using venv built in module Run this command
python -m venv myenv

Activate the Virtual Environment

  • To activate the virtual environment do this command:

On windows

.\myenv\Scripts\activate

On Mac/Linux

myenv/bin/activate

After this command you will now see your python virtual environment on the left side of your command prompt

Install Dependencies

  • You can now install any package you want using pip to list all the pip package available use this command:
pip list

And you will see all the package available to install and to update just follow the command you will see on your command prompt

Deactivate the Environment

  • After all things you do on your python virtual environment now you can deactivate it using this command:
deactivate

Managing Virtual Environments with Tools like Pipenv and Poetry

  • Pipenv - Pipenv shell provides a seamless way to enter the isolated environment tailored to your project. Any packages you install or commands you run while the shell is active will be contained within that virtual environment.

  • Poetry - Unlike other tools like pip and virtualenv, Poetry is a single tool that offers dependency management, packaging, and virtual environment creation. Poetry replaces setup.py, requirements.txt, setup.cfg, MANIFEST.in and Pipfile with a simple pyproject.toml based project format.

What is Python package manager?

  • A Python Package Manager is an essential tool for managing Python software packages and libraries. It automates the processes of installing, upgrading, configuring, and managing Python packages, simplifying the development and maintenance of Python projects.

  • The most widely known package manager is pip (which stands for "Pip Installs Packages"). Pip is the default and standard tool for managing packages from the Python Package Index (PyPI), which is a repository containing thousands of Python libraries. Pip simplifies the installation of packages and handles dependencies by resolving and downloading any needed packages automatically

Additional Features:

  • Pipenv - Pipenv is a Python virtualenv management tool that supports a multitude of systems and nicely bridges the gaps between pip, python (using system python, pyenv or asdf) and virtualenv.

  • Poetry - Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Poetry offers a lockfile to ensure repeatable installs, and can build your project for distribution.

  • Conda - Conda provides package, dependency, and environment management for any language.

How to manage your dependencies correctly

  1. Use .gitignore to your root project so that it will not pushed on your Github Repository inside the .gitignore file add this text:

     venv/
    
  2. Generate a requirements.txt this file will be your saving file to all of your dependencies. It will be the guide to anyone who clones your repository so that they will install what they needed dependencies. Run this command on your command prompt inside your root project folder:

     pip freeze > requirements.txt
    
  3. To clone or update the repository from your Github Repository and install all the needed dependencies by someone or you run this command:

     pip install -r requirements.txt
    

This management will also be your best practice so that you will avoid pushing unnecessary file and make other who clone your repository smooth setup.

0
Subscribe to my newsletter

Read articles from Cañete,Brandon L. directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Cañete,Brandon L.
Cañete,Brandon L.