Python Virtual Environments and Packages

Introduction

Packages and modules that aren’t included in the standard library are frequently used in Python applications. Applications may require a specific version of a library because the application requires the fix of a specific issue or because the application was created using an obsolete version of the library’s interface.

This means that a single Python installation may not be able to cover the needs of all applications. If application A requires version 1.0 of a module and application B requires version 2.0, the requirements are incompatible, and installing either version 1.0 or 2.0 will prevent one of the applications from running.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

Installing Virtual Environments

To install Virtual environment:

pip install virtualenv

To check virtual environment is installed:

virtualenv --version

Creating Virtual Environments

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path

python3 -m venv py-env

This will create the py-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter and various supporting files.

Activating Virtual Environments

Once you’ve created a virtual environment, you may activate it.

On Windows, run:

py-env\Scripts\activate.bat

or you can run this command:

py-env\Scripts\activate

On Unix or MacOS, run:

source py-env/bin/activate

Deactivating Virtual Environments

To Deactivate venv you must have venv active then you can run command into your activated venv

deactivate

For example:

(py-env) <project-path> deactivate

Managing Packages with pip

You can install, upgrade, and remove packages using a program called pip. By default pip will install packages from the Python Package Index, <https://pypi.org>. You can browse the Python Package Index by going to it in your web browser.

pip has a number of subcommands: “install”, “uninstall”, “freeze”, etc. (Consult the Installing Python Modules guide for complete documentation for pip.)

You can install the latest version of a package by specifying a package’s name:

(py-env) $ python -m pip install <package-name>

You can also install a specific version of a package by giving the package name followed by == and the version number:

(py-env) $ python -m pip install <package-name>==<version>

You can uninstall packages by package name:

(py-env) $ pip uninstall <package-name>

Display all of the packages installed in the virtual environment:

(py-env) $ pip list

pip freeze : The output of pip freeze is similar to that of pip install, but it utilizes the format that pip install expects. This list is often stored in a requirements.txt file:

(py-env) $ pip freeze > requirements.txt

Once you requirements.txt file was committed you can then install all the necessary packages with install -r:

(py-env) $ python -m pip install -r requirements.txt

That is it for today, hope it helps.  If you have any suggestion for this article please make a comment in comment section below.

If you like this article, you can buy me a coffee. Thanks!

Python Virtual Environments and Packages

One thought on “Python Virtual Environments and Packages

  1. Everything is very open with a precise explanation of the issues. It was definitely informative. Your site is extremely helpful. Thanks for sharing!

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top