Python Runtime Environments

Aus MattWiki

This article tries to bring structure into the different runtime environments for Python for running python programs or Jupyter Notebooks and their advantages and drawbacks:

This site is a major work in progress

General Commands for Runtime Environment Management

The following commands are useful for assessing and and managing Python runtime environments:

Hint: These are always executed in the context of the current runtime environment, as described in the next sections.

pip list                 # Show installed modules
pip install <module>     # Install module

Linux Environments

Debian

Python runtine environment on debian is managed by Debian distribution. This means that you can not install additional packages with pip install <package>. If you try you get the following output:

matt@mypc:~$ pip install matplotlib
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.

    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.

    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.

    See /usr/share/doc/python3.11/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

There are two options to proceed:

Debian Managed Packages

Try and find needed modules in the packages provided by Debian distribution:

apt-get install <package>    # Install package
aptitude                     # Interface for searching and installing available packages

Virtual Environment (venv)

Install additional virtual environment as described in the message above, i.e.:

python3 -m venv ~/venv/finpy

Now either issue commands against that environments as follows:

~/venv/finpy/bin/python script1.py
~/venv/finpy/bin/pip install <package>

A more comfortable option is to switch to the environment sepcified by its path:

source ~/venv/finpy/bin/activate

Example:

matt@mypc:~$ source ~/venv/finpy/bin/activate
(finpy) matt@mypc:~$

All subsequent commands will be executed in the context of the active environment, which is now listed in parentheses.

In order to leave the virtual environment again:

matt@mypc:~$ deactivate

Windows Environments

Windows Native

Miniconda

pip

Known Issues

Not all packages seem to work properly. Known issues:

  • yfinance.download seems not to work properly on Windows