3

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?

Alden Bernitt
  • 119
  • 1
  • 1
  • 7
  • Sounds like an issue with `script1.py` not being present in your `C:\User\Example` directory, as it seems that the `PATH` variable is set up so that `python` can be called from the command line (since you are able to do so). Did you verify that you are running the `python` command from within the same directory your script is located? – wheeler Apr 17 '17 at 14:11
  • @wheeler I'm storing my python scripts and the python interpreter in two separate locations, neither of them being the C:\User\Example directory. I've gone and edited my PATH variable to include to file paths to both the directory holding my interpreter and the directory holding my scripts. For example, my Python interpreter is in the C:\User\Example\Python36 directory and my scripts are in the C:\User\Example\my_scripts directory. – Alden Bernitt Apr 17 '17 at 14:15
  • `neither of them being the C:\User\Example directory` There's your answer. You are calling python from that directory, giving it a name of a file that does not exist in the directory you are currently in. Since its just the name of the file, it **has** to exist in the folder you are currently in for the command to work. In other words, you have to navigate to your `my_scripts` directory with the `cd` utility in order to run `python script1.py`. – wheeler Apr 17 '17 at 15:12

2 Answers2

2

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\%*
  • It would appear that I misread as suspected. Thank you for clarifying this. – Alden Bernitt Apr 17 '17 at 22:08
  • A correctly installed Python 3.6 should associate .py[w] files with the py.exe launcher and pass command-line arguments. The py launcher handles running multiple versions of Python based on the script's shebang line. Add `C:\User\Example\my_scripts` to `PATH` and `.PY` to `PATHEXT`. Then run `script1` directly. – Eryk Sun Apr 17 '17 at 22:19
0

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.

wheeler
  • 2,823
  • 3
  • 27
  • 43
  • So is there no way to create an Environment Variable in the python interpreter such that I am able to have a "default" location for my scripts aside from C:\User\Example such as C:\User\Example\my_scripts so that I am able to make such calls as 'C:\User\Example> python script1.py'? Like I said in the original post, my book mentions a PYTHONPATH variable that seems to function in a similar way to what I'm describing. – Alden Bernitt Apr 17 '17 at 16:21
  • Correct, there is no way to do this. PYTHONPATH does not work in the way that was described in the book. It works for **importing** modules, not executing them. Take a look at [this](http://stackoverflow.com/questions/19917492/how-to-use-pythonpath) SO question and answer that describes a near identical situation, and [here](https://docs.python.org/2/using/cmdline.html#envvar-PYTHONPATH) is the reference manual for further reading. – wheeler Apr 17 '17 at 17:37
  • What book are you using? – wheeler Apr 17 '17 at 17:39
  • I'm using *Learning Python* by Mark Lutz. Perhaps I misunderstood what was happening. I'll go review it. – Alden Bernitt Apr 17 '17 at 18:07