简体   繁体   中英

Beginner question about finding Python script in IDLE shell

I'm a beginner in Python with no prior programming experience, so bear with me here.

I installed Python, started playing with it (typing variables, playing with math operations) in the Shell window and everything is fine. I open a New Window and started writing a simple script. Something like this:

print (1+1)

I press Run Module, and I am asked to name and save the script first. I do so by calling it firstscript.py, and save it to my desktop.

Now I press Run Module, and in the shell window 2 is printed on the screen. Everything is fine. I close Python, and open it up again. Now in the shell window, I type firstscript.py and I get the red message NameError: name 'firstscript' is not defined.

I can run my program only if I open the script file on my desktop and press Run Module from there, but if I try to start it up directly in IDLE Shell by typing its name, I get the error message.

What did I do wrong? Thank you.

Good to see that you are starting with python.

Firstly, you can run the file directly using 'Run Module' only when you have the file open. Once you save the file and quit, you are out of the file editor and back to the terminal. Simply typing in firstscripty.py will not work as it does not recognize the command. To run the file from the terminal, use the below code:

python [locationOfFile\]firstscript.py

You can check out this detailed explanation: https://realpython.com/python-idle/#how-to-work-with-python-files

The problem here is the Shell doesnt know that your firstscript.py is sitting on the desktop

The simplest way i suggest using cmd with:

python C:\Users\{your user}\Desktop\firstscript.py

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM