Learning Python

20. PIP: Using packages and virtual environments

PIP is a package manager for Python for packages or modules

20.1. Create and start the virtual environment

First we create virtual environment

$ python3 -m venv .venv
$

To activate the

$ source .venv/bin/activate
(.venv) $ python
Python 3.9.2 (default, Feb 20 2021, 00:00:00) 
[GCC 10.2.1 20201125 (Red Hat 10.2.1-9)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/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']
>>>

20.2. Managing packages with pip

Since Python 3.4, Pip is installed by default en

$ source .venv/bin/activate
(.venv) $ python -m pip search camelcase
$ source .venv/bin/activate
(.venv) $ pip search camelcase
(.venv) $ pip install camelcase
Collecting camelcase
  Using cached camelcase-0.2.tar.gz (1.3 kB)
Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
Installing collected packages: camelcase
    Running setup.py install for camelcase ... done
Successfully installed camelcase-0.2
(.venv) $ pip install camelcase==0.1
Collecting camelcase==0.1
  Downloading camelcase-0.1.tar.gz (1.2 kB)
Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
Installing collected packages: camelcase
  Attempting uninstall: camelcase
    Found existing installation: camelcase 0.2
    Uninstalling camelcase-0.2:
      Successfully uninstalled camelcase-0.2
    Running setup.py install for camelcase ... done
Successfully installed camelcase-0.1
(.venv) $ pip install --upgrade camelcase
Collecting camelcase
  Using cached camelcase-0.2.tar.gz (1.3 kB)
Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
Installing collected packages: camelcase
  Attempting uninstall: camelcase
    Found existing installation: camelcase 0.1
    Uninstalling camelcase-0.1:
      Successfully uninstalled camelcase-0.1
    Running setup.py install for camelcase ... done
Successfully installed camelcase-0.2
(.venv) $ pip show camelcase
Name: camelcase
Version: 0.2
Summary: Converts a string to Camel Case
Home-page: http://www.craigaddyman.com
Author: Craig Addyman
Author-email: craig.addyman@googlemail.com
License: MIT
Location: /workspaces/python-examples/.venv/lib/python3.9/site-packages
Requires: 
Required-by:
(.venv) $ pip list
Package    Version
---------- -------
camelcase  0.2
pip        20.2.2
setuptools 49.1.3

20.3. Install all dependencies

(.venv) pip freeze>requirements.txt
(.venv) cat requirements.txt
camelcase==0.2
(.venv) $ pip uninstall camelcase
Found existing installation: camelcase 0.2
Uninstalling camelcase-0.2:
  Would remove:
    /workspaces/python-examples/.venv/lib/python3.9/site-packages/camelcase-0.2-py3.9.egg-info
    /workspaces/python-examples/.venv/lib/python3.9/site-packages/camelcase/*
Proceed (y/n)? y
  Successfully uninstalled camelcase-0.2
(.venv) $ pip install -r requirements.txt
Collecting camelcase==0.2
  Using cached camelcase-0.2.tar.gz (1.3 kB)
Using legacy 'setup.py install' for camelcase, since package 'wheel' is not installed.
Installing collected packages: camelcase
    Running setup.py install for camelcase ... done
Successfully installed camelcase-0.2