Wiki/python.md

1.3 KiB

Python

Virtualenv

  • Of specific version:
    • Make sure version is available in /usr/bin/
    • Create env: `virtualenv -p .venv

Other

  • Show packages imported at startup: python -X importtime -m <pythonFile>
    • Useful to verify lazy loading

Profiling

  • Run profiler: python -m cProfile -o out.out -- <FILE> <ARGS>
  • Visualize as flow chart: python -m gprof2dot -f pstats out.out | dot -Tpng -o out.png
    • Deps: gprof2dot
    • By default, excluded negligible nodes/edges. Set --node-thres 0 (-n) and --edge-thres 0 (-e) to include all
  • Visualize as starburst (interactively): python -m snakeviz out.out

Profile single function/method

  • Import from profilehooks import profile
  • Add @profile to function to profile

Make some local module available to another module

Useful for developing a plugin

  • Create symlink to that moduel in .venv/lib/python3.11/site-packages/

Paths

Use paths that are relative to the source file

  • Define BASE = Path(__file__).parent.resolve()
  • Define all paths relative to BASE: BASE / "bla.bla"