Stefan Boos

My personal GitHub pages

Follow me on GitHub

Python

Table of Contents generated with DocToc

Communities

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:

  1. Install pyenv
  2. 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.

  3. Now you can switch the global python version to the installed one: pyenv global 3.12

  4. 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:

Python Environment on the Right of Command Prompt

To set this up, do this in your ~/.zshrc (adopted from Stackoverflow):

plugins=(virtualenv)

POWERLEVEL9K_RIGHT_PROMPT_ELEMENTS=(status virtualenv)

Caveats:

  1. Add that plugin in addition to other plugins you have.
  2. 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

Interesting Packages

Creating Packages