Eggs in path before PYTHONPATH environment variable

前端 未结 3 1199
长情又很酷
长情又很酷 2020-12-28 18:08

If I have packages installed from easy_install, the eggs are prepended to sys.path before the items in the PYTHONPATH variable.

Fo

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-28 18:46

    Consider using the -S command-line option to suppress *.pth processing:

    python -c 'import sys; print("\n".join(sys.path))'
    python -S -c 'import sys; print("\n".join(sys.path))'
    

    https://docs.python.org/3/library/site.html#site.main

    You can also use -S with site.main() to delay *.pth processing until runtime, say to capture the original sys.path for appending:

    export PYTHONPATH=$(
      PYTHONPATH='' \
      python -c 'import sys; \
        sys.path.extend(sys.argv[1:]); old=list(sys.path); \
        import site; site.main(); \
        [ old.append(p) for p in sys.path if p not in old ]; \
        sys.path=old; \
        print ":".join(sys.path)' \
      $EXTRA_PATH $ANOTHER_PATH)
    
    python -S ... # using explicit PYTHONPATH
    
    • Start from explicit empty PYTHONPATH
    • Append to sys.path explicitly with extend
    • Import site and call site.main()
    • Append new paths to old path and then install it in sys.path
    • Print with ":" for PYTHONPATH
    • python -S is desirable for later runs only using $PYTHONPATH
    • python -S may or may not be desirable while setting PYTHONPATH (depending on if you need sys.path expanded before extending)

提交回复
热议问题