Python Library Paths

One of my newbie peeves is having Python find a new library I may download. This involves 'sys.path'

  • a couple brief mentions

    • this mentions Python Path and other inputs

      • this suggests setting Python Path in 'autoexec.bat' (in Ms Win; run 'msconfig'); also check the Registry (not sure how the 2 cases interact) and environment variables; everyone is confused; here's a place to look if all else fails. This recommends not changing Python Path anywhere.
  • does python look in the subdirectories of the entries in 'sys.path'? What should go inside the 'site-packages' directory , and what shouldn't?

    • I think only 3rd-party packages should go in here
  • 'sys.path.append' only alters 'sys.path' while Python is running. How make it stick?

    • '.pth' file? see this and this and this

      • this doesn't let you set any full directory - the entries in the .pth file are names of subdirectories of the directory containing the .pth file itself; and that file must sit in one of a few directories:

        • in UNIX - in /usr/lib/site-python/ (note this is outside the directory holding python itself) (or in '.../site-packages/' subdirectory of python)

        • in Ms Windows - probably the directory of python itself (check sys.prefix and sys.exec_prefix values)

      • and my testing says that you can't nest directories with levels of .pth files

      • and I'm pretty sure searching doesn't walk down subdirectories (reading Learning Python about how packages can be imported by directory reference - if 'A' is on search path, then you can 'import A.B.C' to import a module 'C' within subdirectory 'B' of 'A')

        • this disagrees - Paths can be absolute or relative, in which case they're relative to the directory containing the .pth file. Any directories added to the search path will be scanned in turn for .pth files.
  • a couple pieces on which variant of 'import' to use

  • my conclusion:

    • don't touch Python Path registry or environment variables

    • put 3rd-party libraries in 'site-packages' unless they tell you otherwise

    • make a 'seitz' subdirectory of main python directory, plus a 'seitz.pth' file to point to it; do same for each separate employer

Edited: |

blog comments powered by Disqus