Reputation: 139
I've just started learning Python using Learning Python by Mark Luts. In his book he offers an example of a simple script that is called through the Windows shell. In the example, he calls is as follows:
C:\code> python script1.py
I've gone and modified the Environment Variables on my machine so that I can call
C:\User\Example> python
to open up the interpreter and I can also call something like
C:\User\Example> script1
to run a script that I've written and placed in my desired directory. My issue is that I can not call
C:\User\Example> python script1.py
in my command line the same way he does in the book. He's mentioned something about a PYTHONPATH Environment Variable, however, this variable isn't present on my machine. I only have 'path', 'TEMP', and 'TMP'. Particulary, when I try to make such a call I get the error
python: can't open file 'script1.py': [Errno 2] No such file or directory
What do I have to do in order to get this sort of command to work properly on the command line?
Upvotes: 5
Views: 25455
Reputation: 11
it should use (") in your path to your script. for example :
python "C:\Users\Acer\palm-recognizition\src\predict.py"
Upvotes: 1
Reputation:
From the book (p. 44, 4th Ed):
Finally, remember to give the full path to your script if it lives in a different directory from the one in which you are working.
For your situation, this means using
C:\User\Example> python C:\User\Example\my_scripts\script1.py
You could write a batch file that looks for the script in a predefined directory:
@echo off
setlocal
PATH=C:\User\Example\Python36;%PATH%
SCRIPT_DIR=C:\User\Example\my_scripts
python %SCRIPT_DIR\%*
Upvotes: 3
Reputation: 3241
You are calling python
from within the context of C:\User\Example
, and passing it a name of a file you want to run through the intepreter (script1.py
). It is clear that the PATH
variable is setup correctly such that you can call python
from anywhere on you computer, since we can see that it is running but not actually able to find your script.
However, you stated in the comment that your scripts are actually located in C:\User\Example\my_scripts
. In other words, you are passing python
a name of a file that doesn't exist!! (at least from the contect of C:\User\Example
).
You need to be in the directory of the script in order for the python
executable to be able to find it.
Alternatively, you can run the python command and give it more information as to where the script is. For instance, you could run python .\my_scripts\script1.py
if you are running from within the contect of C:\User\Example
and your scripts are in C:\User\Example\my_scripts
.
Upvotes: 0