Python

Website
www.python.org

Standard library
Python 2
Python 3

First Steps

  • Use a Python Virtual environment
    If you are programming something more complex, use some python-packages not available in a simple python-environment, you should create a virtual-environment and store the used python-packages in a requirements.txt
  • Just one or two python packages
    for personal use in some automation-scripting can be installed in a user environment.
    The default user environment is located in ~/.local/lib but by setting some environment variables it can be moved somewhere else (You can add the following line to your .bashrc file - but you should not):
.bashrc
...
export PYTHONUSERBASE=~/mypylibs
export PYTHONPATH=$PYTHONUSERBASE:$PYTHONPATH
...
To install some packages you can now use the pip command:
pip install --user SomePackage

Little hints and hacks

Buffering

In case you want to continuously pipe the standard output of the terminal (STOUT) into a file then the output is buffered. When outputting to a terminal, STDOUT will be line-buffered or not buffered at all, so you'll see output after each \n or for each character.

You can use the option -u to prevent python from buffering the output stream.

python3 -u ${SCRIPT_FILE} > ${FILE_TO_WRITE} 

Or set an environment variable PYTHONUNBUFFERED=1, (this is what PyCharm uses)

PYTHONUNBUFFERED=1 python3 ${SCRIPT_FILE} > ${FILE_TO_WRITE} 

When you always want to execute a python-script in unbuffered mode you can add it to the Shebang at the beginning of your script.

#!/usr/bin/env python3 -u
Performance

Completely unbuffered output can slow down your simulation (creates a lot system-calls).

0-buffering, equal to PYTHONUNBUFFERD or -u:

stdbuf -o0  python3 ${SCRIPT_FILE} > ${FILE_TO_WRITE} 

L-buffering; Linebuffer, a complete line is buffered (best)

stdbuf -oL  python3 ${SCRIPT_FILE} > ${FILE_TO_WRITE} 

If it is not necessary to see all output written immediately, you can use user-defined buffers: K Kilobyte, M Gigabyte or even G Gigabyte.

Tutorials

Further Resources

Log In