Virtual Environments and Packages

12.1. Introduction

What is Virtual Environment?

a self-contained directory tree that contains a Python installation for a particular version of Python, plus a number of additional packages.

Why I need virtual environments?

You may develop different applications, they may need various packages, and different packages may need different version of Python

12.2. Creating Virtual Environments

The module used to create and manage virtual environments is called venv

  • venv will usually install the most recent version of Python that you have available.(from your system, not the Internet)

  • you can select a specific Python version by running python3 or whichever version you want.

    1
    2
    $ python3 -m vene tutorial-env
    # pythonversionyouwant -m vene thedirectoryyouchoose

    Note that Python2 do not have module vene

  • This will create the tutorial-env 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.

  • .venv is a directory which is often used.

  • to active the environment

    1
    2
    3
    # before
    $ python --version
    Python 2.7.16
    1
    2
    $ source tutorial-env/bin/activate # for unix
    # tutorial-env\Scripts\activate.bat # for windows
    1
    2
    3
    # after
    (tutorial-env)$ python --version
    Python 3.9.1
  • no package is installed except built-in modules

    1
    2
    3
    4
    5
    (tutorial-env) $ pip list        
    Package Version
    ---------- -------
    pip 20.2.3
    setuptools 49.2.1

    12.3. Managing Packages with pip

You can install, upgrade, and remove packages using a program called pip.

  • default install source https://pypi.org

  • search package in your browser or use a limited feature via pip

    1
    2
    3
    $ pip search packagename # this will not be support any more
    ERROR: XMLRPC request failed [code: -32500]
    RuntimeError: PyPI's XMLRPC API is currently disabled due to unmanageable load and will be deprecated in the near future. See https://status.python.org/ for more information.
  • install a specificated version of a package

    1
    2
    $ pip install requests==2.6.0
    # $ pip install packagename==version
  • upgrade a specificated package

    1
    2
    $ pip install --upgrade requests
    # python -m pip install --upgrade packagename
  • pip uninstall followed by one or more package names will remove the packages

  • pip show packagename will display information about a particular package

  • pip list will display all of the packages installed

  • pip freeze will produce a similar list of the installed packages, but the output uses the format that pip install expects. A common convention is to put this list in a requirements.txt file

    1
    2
    3
    4
    5
    (tutorial-env) $ pip freeze > requirements.txt
    (tutorial-env) $ cat requirements.txt
    novas==3.1.1.3
    numpy==1.9.2
    requests==2.7.0
  • Users can then install all the necessary packages with install -r

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    (tutorial-env) $ python -m pip install -r requirements.txt
    Collecting novas==3.1.1.3 (from -r requirements.txt (line 1))
    ...
    Collecting numpy==1.9.2 (from -r requirements.txt (line 2))
    ...
    Collecting requests==2.7.0 (from -r requirements.txt (line 3))
    ...
    Installing collected packages: novas, numpy, requests
    Running setup.py install for novas
    Successfully installed novas-3.1.1.3 numpy-1.9.2 requests-2.7.0
  • see more for pip at Installing Python Modules

  • If you want to develop a package, consult Distributing Python Modules