brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -m site shows sys.path and the user site-packages folder

Python
~/tips/tip-python-m-site-user-site

python -m site

Prints the module search path, the user base and user site folders, and whether the user site is enabled.

When an import fails and I cannot tell which Python is looking where, I want the search path on screen before I start guessing. The site module can print it from the command line.

python -m site
python -m site --user-site

Without options the output lists the contents of sys.path, then USER_BASE and USER_SITE with a note whether each folder exists, and finally ENABLE_USER_SITE. That last line tells you whether per-user packages (the ones from pip install --user) are visible to this interpreter at all.

With --user-base or --user-site it prints just that path, which is handy in a script. Give it both and you get the two paths separated by os.pathsep.

A typical use: you installed a package, Python says it does not exist, and the path list shows that you are running the interpreter of a different virtual environment than you thought.

The options also set the exit code, which is a neat trick for a quick check. Code 0 means the user site-packages directory is enabled, 1 means it was switched off by you (the -s flag or PYTHONNOUSERSITE), and 2 means it was disabled for security reasons or by an administrator. Anything above 2 is an error.

The caveat is that the output belongs to the interpreter you start. python, python3 and the Python inside a venv can all give different answers, so run it with the same command you use for the failing script.

Further reading: Python docs: site, command line interface

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link