0

I'm trying to get python to run my scripts via CMD line.
note: idr if the book said where (a specific place to store my files for) python to access them but scanning back over the beginning I didn't find any relation to it.

According to this book https://automatetheboringstuff.com/2e/appendixb/ I am supposed to be able to type python (script.py) in command line just like this and it should run the script:

Here's the error I am getting upon execution, compared to the example from the book below it to show that this is supposed to work.

CMD LINE OUTPUT :

Microsoft Windows [Version 10.0.18362.1016]
(c) 2019 Microsoft Corporation. All rights reserved.

C:\Users\Armagon>python conway.py
python: can't open file 'conway.py': [Errno 2] No such file or directory

C:\Users\Armagon>

As you can see I get a python Error and researching this has given me nothing I found useful.

Here is the exact sample quoted from the book:

Microsoft Windows [Version 10.0.17134.648]
(c) 2018 Microsoft Corporation. All rights reserved.

C:\Users\Al>python hello.py
Hello, world!

C:\Users\Al>

First off, doing some research I have found a way to make it work, by cd(change directory) to the direct path of the folder containing the scripts C:\Users\Armagon\Desktop\mystuff. This helps to verify that the script I am trying to run is in fact located there and I've spelled it correctly.

But, according to everything I've followed up to this point I shouldn't have to do it this way.

The scripts are on my desktop all in the same folder called mystuff.
Following this link as well as a video Add a directory to Python sys.path so that it's included each time I use Python
I added the mystuff folder to PYTHONPATH in environment variables exactly as suggested. Here is a partial output of the sys.path (ran from IDLE) that shows mystuff filepath is added, so I'm pretty sure I did that part right.

['', 'C:\\Program Files\\Python38\\Lib\\idlelib', 'C:\\Users\\Armagon\\Desktop\\mystuff']

Maybe I've confused what is supposed to be happening here I am operating under the assumption (based off what I've learned so far) that when CMD executes the line: python (script).py
It's supposed to run python.
Which in turn python is supposed to find my scripts on desktop (by the PYTHONPATH environment variable I created) and execute the script typed in CMD.

I've tried a lot of things in the process of getting just this far. I'd really appreciate if someone could point out what it is I'm overlooking to run python scripts from my desktop just like the book. I'm not very skilled at present and will gladly add any information as needed if I have excluded pertinent info I apologize in advance.

Armagon
  • 58
  • 10
  • 1
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Ken White Aug 15 '20 at 04:58
  • 1
    TY! When seeking help, I want to make it as easy possible for people to do so. I can see that using 'img' can have the exact opposite impact of what I intended. Though this has been answered I took the time to edit it. – Armagon Aug 15 '20 at 19:08

2 Answers2

1

You should be able to do this as long as the script is in the module search path, for example, the PYTHONPATH environment variable. You should then be able to run the script like this:

python -m script
StarbuckBarista
  • 1,298
  • 1
  • 8
  • 20
  • that isn't working doesn't seem to give me much to go on either C:\Program Files\Python38\python.exe: No module named conway I was already assuming python was running and the one searching for the module ty for the idea though – Armagon Aug 15 '20 at 05:31
  • this did work I am accepting your answer I made a mistake I changed the PYTHON environment variable when I was trying different things ty soooo much! – Armagon Aug 15 '20 at 06:16
0

Well, the cd way is the way to go to run a python file via the command prompt. There is no other way I can think of. Follow these steps or this link -> https://www.wikihow.com/Use-Windows-Command-Prompt-to-Run-a-Python-File to do it:

1)Open command prompt. type cmd or command prompt in search bar or run and press enter key.

2)In command prompt, given that the folder you are trying to access is in desktop, type-> cd desktop\myStuff.. Press enter.

3)Just write- python filename.py and enter.

4)It should run automatically.