How can I use a Python script in the command line without cd-ing to its directory? Is it the PYTHONPATH?

后端 未结 5 1334
后悔当初
后悔当初 2020-11-22 11:20

How can I make any use of PYTHONPATH? When I try to run a script in the path the file is not found. When I cd to the directory holding the script the script runs. So what go

5条回答
  •  孤独总比滥情好
    2020-11-22 11:34

    PYTHONPATH only affects import statements, not the top-level Python interpreter's lookup of python files given as arguments.

    Needing PYTHONPATH to be set is not a great idea - as with anything dependent on environment variables, replicating things consistently across different machines gets tricky. Better is to use Python 'packages' which can be installed (using 'pip', or distutils) in system-dependent paths which Python already knows about.

    Have a read of https://the-hitchhikers-guide-to-packaging.readthedocs.org/en/latest/ - 'The Hitchhiker's Guide to Packaging', and also http://docs.python.org/3/tutorial/modules.html - which explains PYTHONPATH and packages at a lower level.

提交回复
热议问题