Python
Table of Contents generated with DocToc
Communities
- PyData - A community for developers and users of open source data tools.
- Python User Group Austria
Links
-
pytest - newer unit testing framework.
-
unittest - unit testing framework.
-
Structuring Your Project - suggested Python 3 project structure.
-
Implementing an Interface in Python - informal and formal interfaces and abstract base classes.
-
Data Classes - mutable named tuples with default values.
-
PEP 8 – Style Guide for Python Code - Python code style recommendations.
-
Python Developer’s Guide - The guide for developers of Python.
Installing Python on macOS
Note, that macOS comes with Python 2.7 pre-installed. If you are new to Python, I recommend using version 3 instead.
Further note, that I am managing my python installations using pyenv and I am
developing code using JetBrains PyCharm. This combination leads to
using pyenv
in parallel with the Python built in venv …
and looks strange to me. Probably I’ll find a better solution later.
To install Python 3 on macOS the following steps are recommended:
- Install pyenv
- Follow the instructions in Homebrew in macOS
- Pay special attention to the verification step of Build Environment einrichten
-
The installation instructions end with installing a python version. Choose the version number of the latest stable Python build here (at the time of writing this was version
3.12
):pyenv install 3.12
. -
Now you can switch the global python version to the installed one:
pyenv global 3.12
- Restart your shell and verify the
python --version
Remember:
To re-activate the pre-installed python use pyenv global system
Please note that when using pip
for installing Python applications, you may need to add $HOME/.local/bin
to your $PATH
.
Display the Current venv in Command Prompt
I am using Oh my ZSH with the Powerlevel9k theme. This allows printing the current venv as a part of the command prompt:
To set this up, do this in your ~/.zshrc
(adopted from Stackoverflow):
plugins=(virtualenv)
POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status virtualenv)
Caveats:
- Add that plugin in addition to other plugins you have.
- Requires the POWERLEVEL9K theme
Pyenv Commands
… are described in the Command Reference.
# list currently installed versions
pyenv versions
# list common 2.x versions available (excludes other vendors)
pyenv install -l | grep '^\s*2\.' | sort -g
# activate a python version globally
pyenv global 3.10.3
Style Guides
Documenting Python Code
- Python Developer’s Guide: Documenting Python
- JetBrains PyCharm Guide: Documenting Code
- Google Python Styleguide: Docstrings
- numpy Styleguide: Docstring Standard
- sphinx.ext.napoleon: Support for NumPy and Google style docstrings
Interesting Packages
- GitHub: JulienPalard / Pipe - Pipelines / stream processing for python.
- Monkeypatching/mocking modules and environments - safely set/delete an attribute, dictionary item or environment variable, or to modify sys.path for importing.
Creating Packages
- Rogier van der Geer: A Practical Guide to Setuptools and Pyproject.toml - The “new” way of setting up python projects.
- Rogier van der Geer: A Practical Guide to Using Setup.py - An older way of setting up python projects.