Packages and virtual environments#

PIP is a package manager for Python for packages or modules

Create and start the virtual environment#

Since Python 3.3, you can create and start a virtual environment as the venv module is now part of the standard library. We will use the venv module to create a virtual environment in the current directory.

1 $ python3 -m venv .venv
2 $

In the bin directory of the virtual environment, there is a file called activate. This file contains the command to activate the virtual environment with the Bash or Zsh shell.

1 $ source .venv/bin/activate
2 (.venv) $ python
3 Python 3.9.2 (default, Feb 20 2021, 00:00:00)
4 [GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
5 Type "help", "copyright", "credits" or "license" for more information.
6 >>> import sys
7 >>> sys.path
8 ['', '/usr/lib64/python39.zip', '/usr/lib64/python3.9', '/usr/lib64/python3.9/lib-dynload', '/workspaces/python-examples/.venv/lib64/python3.9/site-packages', '/workspaces/python-examples/.venv/lib/python3.9/site-packages']
9 >>>

Managing packages with pip#

Since Python 3.4 the pip package is part of the standard library and can be called as a module or as an executable.

Example:

1 $ source .venv/bin/activate
2 (.venv) $ python -m pip search camelcase
3 (.venv) $ pip search camelcase

Note

The pip package can only be upgraded when it is called as a module.

Warning

Since late 2020 the search functionality of the pip package has been deprecated due to performance issues on PyPi infrastructure.

Example:

1 (.venv) $ pip install camelcase
2 Collecting camelcase
3 Using cached camelcase-0.2.tar.gz (1.3 kB)
4 Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
5 Installing collected packages: camelcase
6     Running setup.py install for camelcase ... done
7 Successfully installed camelcase-0.2

Example:

 1 (.venv) $ pip install camelcase==0.1
 2 Collecting camelcase==0.1
 3 Downloading camelcase-0.1.tar.gz (1.2 kB)
 4 Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
 5 Installing collected packages: camelcase
 6 Attempting uninstall: camelcase
 7     Found existing installation: camelcase 0.2
 8     Uninstalling camelcase-0.2:
 9     Successfully uninstalled camelcase-0.2
10     Running setup.py install for camelcase ... done
11 Successfully installed camelcase-0.1

Example:

 1 (.venv) $ pip install --upgrade camelcase
 2 Collecting camelcase
 3 Using cached camelcase-0.2.tar.gz (1.3 kB)
 4 Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
 5 Installing collected packages: camelcase
 6 Attempting uninstall: camelcase
 7     Found existing installation: camelcase 0.1
 8     Uninstalling camelcase-0.1:
 9     Successfully uninstalled camelcase-0.1
10     Running setup.py install for camelcase ... done
11 Successfully installed camelcase-0.2

Example:

 1 (.venv) $ pip show camelcase
 2 Name: camelcase
 3 Version: 0.2
 4 Summary: Converts a string to Camel Case
 5 Home-page: http://www.craigaddyman.com
 6 Author: Craig Addyman
 7 Author-email: [email protected]
 8 License: MIT
 9 Location: /workspaces/mastering-python/.venv/lib/python3.9/site-packages
10 Requires:
11 Required-by:

Output:

1 (.venv) $ pip list
2 Package    Version
3 ---------- -------
4 camelcase  0.2
5 pip        20.2.2
6 setuptools 49.1.3

Install all dependencies#

Example:

1 (.venv) $ pip freeze > requirements.txt
2 (.venv) $ cat requirements.txt
3 camelcase==0.2

Example:

1 (.venv) $ pip uninstall camelcase
2 Found existing installation: camelcase 0.2
3 Uninstalling camelcase-0.2:
4 Would remove:
5     /workspaces/mastering-python/.venv/lib/python3.9/site-packages/camelcase-0.2-py3.9.egg-info
6     /workspaces/mastering-python/.venv/lib/python3.9/site-packages/camelcase/*
7 Proceed (y/n)? y
8 Successfully uninstalled camelcase-0.2

Example:

1 (.venv) $ pip install -r requirements.txt
2 Collecting camelcase==0.2
3 Using cached camelcase-0.2.tar.gz (1.3 kB)
4 Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
5 Installing collected packages: camelcase
6     Running setup.py install for camelcase ... done
7 Successfully installed camelcase-0.2