Skip to content

This document is a WORK IN PROGRESS.
This is just a quick personal cheat sheet: treat its contents with caution!


Python

Python is an interpreted, high-level, general-purpose programming language, first released in 1991. Python's design philosophy emphasizes code readability with its notable use of significant white space. Its language constructs and object oriented approach aim to help programmers write clear, logical code for small and large-scale projects.

Reference(s)

Table of contents

* [Install](#install)
* [Config](#config)

Install

# emerge -a dev-lang/python
# pacman -S python
# apt install python3
# yum install python
# dnf install python

Config

TODO

Managing multiple Python versions

TODO (with eselect?)

TODO

List the different alternatives you have:

$ update-alternatives --list
    > python                  auto    /usr/libexec/no-python
    > ...
    > python3                 auto    /usr/bin/python3.6
    > ...
Select an alternative:
$ sudo update-alternatives --config python
    There are 5 programs which provide 'python'.

      Selection    Command
    -----------------------------------------------
    *+ 1           /usr/libexec/no-python
       2           /usr/bin/python3
       3           /usr/bin/python3.8
       4           /usr/bin/python3.9
       5           /usr/bin/python2

    Enter to keep the current selection[+], or type selection number:
Check the result:
$ python --version


Use

TODO

Warning

Try to not use $ pip install ... system wide. Prefer your system package manager instead whenever possible.

Tip

If $ pip install ... is needed: then use it in a virtual environment (e.g. with venv).

Tip

If $ pip insall ... is needed outside a virtual environment: then use the --user flag, in order to not install system wide but only user wide! This is important because a system wide installation could conflict with some packages.

Warning

Never update pip without your package manager, i.e. never run $ pip install --upgrade pip or $ python -m pip install --upgrade pip outside a virtual environment.

Tip

If you did updated pip outside a virtual environment, then you can recover a working pip with: $ sudo python -m pip uninstall pip, and then uninstall/re-install the pip package with your package manager (e.g. $ sudo apt --reinstall install python-pip). See https://github.com/pypa/pip/issues/5599.

  • List pip installed modules

    $ python -m pip list
    

  • Uninstall package

    $ python -m pip uninstall package-name
    

How to compile explicitly

  • Compile explicitly, .pyc compiled files will land in __pycache__:

    $ python -m compileall -f .
    

  • Compile explicitly, .pyc compiled files will land next to their .py source files:

    $ python -m compileall -f -b .
    

  • The major advantage with the last compilation method is that you can easily run the .pyc compilation files:

    $ python -m compileall -f -b . && python main.pyc
    

Packaging

How to package a Python program into a single file or folder with its dependencies

See pyinstaller cheat sheet.

Python virtual environment (venv) good practices

Tip

It's a good practice to have a dedicated venv for a project needing python dependencies.

  • Create and enter a venv:

    $ python -m venv pyenv
    $ source ./pyenv/bin/activate
    

  • Update pip:

    (pyenv) $ python -m pip install --upgrade pip
    

  • Exit the pyenv:

    $ deactivate
    

pyenv

pyenv lets you easily switch between multiple versions of Python: https://github.com/pyenv/pyenv.

Poetry

See poetry cheat sheet.

Python packaging

TODO

Reference(s)

Conda (with Miniconda)

See Conda cheat sheet.


If this cheat sheet has been useful to you, then please consider leaving a star here.