I feel a little foolish that I don't know this, but I tried to do it today and was surprised when it didn't work....
I have a directory C:\\test
with a demo script, lets call it demo.py
C:\\test
then I can just do python demo.py
. Easy C:\\
, it's python test\\demo.py
What if C:\\test is on the path?
I was expecting to be able to now do python demo.py
from anywhere however...
python: can't open file 'demo.py': [Errno 2] No such file or directory
I feel foolish because I thought this was straightforward, but I have searched around and have not found a solution. Am I fundamentally misunderstanding something here about how the Python interpreter finds scripts to run? I don't think this is anything to do with PYTHONPATH, as I understood that to relate to loading of modules inside scripts.
This is on Windows 7, by the way.
The PATH is only used to search for commands. A first way is that a Python script can be used directly as a command and in that case the PATH will be used: just use demo.py
instead of python demo.py
.
It will rely on OS specific ways. On Windows, file type (given by the extension - here .py) can be given default application to process them, while on Unix-like, the first line of a script can declare the program that will process it.
Alternatively, python allows to launch a module that will be searched in the PYTHONPATH ( not PATH) by using python -m module
or for Windows py -m module
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.