norboten · cheat sheet

Where Python looks for a module, and who gets there first — cheat sheet

python, linux-basics
python3 -c 'import sys; print(sys.path)'                     # the search path, in order
python3 -c 'import calendar; print(calendar.__file__)'       # which file an import used
python3 -c 'import sys, pathlib; print([p.name for p in pathlib.Path(".").glob("*.py")
    if p.stem in sys.stdlib_module_names])'                  # names that will shadow
python3 -P script.py            # do not put the script's directory on the path
find . -name __pycache__ -prune -exec rm -rf {} +            # after a rename
Started as sys.path[0]
python3 /path/script.py /path — the script’s directory
python3 -c · python3 -m · the REPL the current directory