Thanks to theidioms.com

Learn Python For Data Science (Course I)

Learn Python For Data Science (Course I)

Virtual Environments and Packages in Python

As discussed in the previous chapter, Python provides a decent set of standard libraries for a range of tasks. However, Python applications often require packages and modules that don’t come as part of the standard library. There are cases when applications need a specific version of a library since the application may be written using a specific version of the library. Hence, it is not possible for one Python installation to meet the requirements of every application.

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. With these, different applications can then use different virtual environments as per requirement.

Creating virtual environments

To create a virtual environment, decide upon a directory where you want to place it, navigate to that directory from command line/terminal and run the venv module as:

python3 -m venv myenv

This will create the myenv directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files. A common directory location for a virtual environment is .venv.

The directory tree of an environment looks like the following:

project-directory/myvenv/                  # Environment's root directory
├── bin
│   ├── activate                           # Scripts to activate the virtual environment
│   ├── activate.csh                       
│   ├── activate.fish                     
│   ├── easy_install
│   ├── easy_install-3.7
│   ├── pip
│   ├── pip3
│   ├── pip3.7
│   ├── python -> /usr/local/bin/python    # Symlinks to system-wide
│   └── python3 -> python3.8               # Python instances.
├── include
├── lib
│   └── python3.8
│       └── site-packages                  # Stores local site packages
└── pyvenv.cfg

Activating virtual environments

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

For Windows, run:

myenv\Scripts\activate.bat

For Unix or MacOS, run:

source myenv/bin/activate

After activation of a virtual environment, every python code you run will use the python interpreter of this environment.

Managing Packages

1. Manually installing/uninstalling new packages

You can install, upgrade, and remove packages using a program called pip which is a package manager for Python. By default, pip will install packages from the Python Package Index, https://pypi.org.

The program manager pip has a number of subcommands: “search”, “install”, “uninstall”, “freeze”, etc. You can install the latest version of a package by specifying a package’s name.

pip install package_name

For example, package_name can be any package known by pypi such as numpy, pandas, matplotlib, etc. So, to install numpy, the following command can be used:

pip install numpy

Conversely, you can uninstall the package using the uninstall command of pip.

pip uninstall package_name

2. Using the requirements.txt file

A requirements file is a text file that lists out all the external packages/modules and their version number that are installed in a particular virtual environment. Requirements file comes handy when we need to replicate a virtual environment in some other computer(s) so that an application runs perfectly on the other computer(s) too.

To generate a requirements file for a virtual environment, run the following command when the environment is activated.

pip freeze > requirements.txt

After running this command, a new file named requirements.txt will be created that lists all the modules installed in that environment. All the packages listed on the requirements.txt file can be easily installed on a new virtual environment by running the following command:

pip install -r requirements.txt

Leave your thought here

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

Close Bitnami banner
Bitnami