~~NOTOC~~
====== Python ======
**Website**\\
[[http://www.python.org|www.python.org]]
**Standard library**\\
[[http://docs.python.org/2/library/|Python 2]]\\
[[http://docs.python.org/3/library/|Python 3]]
==== First Steps ===
* **Use a [[programming:python:virtualenv|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 [[programming:python:requirements-file|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):
...
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 =====
* [[https://pythonmonk.com|pythonmonk]] (interactive tutorial)
* [[https://wiki.python.org/moin/BeginnersGuide|Beginner's Guide to Python]] (Python 2)
* [[http://learnpythonthehardway.org/book/|Learn Python the Hard Way]] (Python 2)
* [[http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_3|Non-Programmer's Tutorial for Python 3]] (Python 3)
===== Further Resources =====
* [[http://legacy.python.org/dev/peps/pep-0008/|PEP 8 -- Style Guide for Python Code]]
* [[http://www.pythontutor.com/visualize.html|Online Python Tutor -- Visualize program execution]]
* Developing and testing Python Apps for [[https://developer.fedoraproject.org/tech/languages/python/multiple-pythons.html|multiple versions]] of Python.
{{tag>"python"}}