A newbie's question: How to run the code?

So I’m taking an online course, in which the teacher shows how to run the code in Python 3.5.0b4. But in the program I’m using, Python 3.8.3 Shell, I see no such option:


So how do I run the code in Python 3.8.3 Shell? Thanks.

This works:

exec(open('full-path-and-filename').read())

A cautionary note:
The Python shell uses the UNIX forward slash, even if you’re running the Windows version (where backslash is used). An example of a full file-and-path-name is:

D:/programming/python/my_python_script.py

The same approach works from a Windows Command Prompt and, again, using the UNIX-style slash.

What’s actually going on here is a three-stage process. First, you access the OS and set up a file handle to the script in question. Second, you read the script file in, and finally, you execute it. Here are the stages separated for clarity:

file_handle = open('000_no_window.py')
my_script = file_handle.read()
exec(my_script)

In the teacher’s screenshot, they’re editing a file in IDLE. In your screenshot, you’re at IDLE’s shell. Try opening your program in IDLE using File → Open. Then, use the same Run → Run Module.

Thanks a lot!

1 Like