Appearance
Using Text Editor
Writing programs directly in Python's interactive command line has the advantage of getting immediate results, but the downside is that you cannot save your work. The next time you want to run your program, you'll have to retype it.
Therefore, in actual development, we always use a text editor to write code, save it as a file, and then the program can be run repeatedly.
Now, let's write the 'hello, world' program from last time using a text editor and save it.
So, Which Text Editor is the Best?
Visual Studio Code!
We recommend Visual Studio Code, developed by Microsoft. It is not the bulky Visual Studio but a lightweight, mini version of Visual Studio that is cross-platform! It works on Windows, Mac, and Linux.
Please note: Do not use Word or the built-in Notepad in Windows. Word does not save plain text files, and Notepad will automatically add a few special characters (UTF-8 BOM) at the beginning of the file, which can cause inexplicable errors when running the program.
After installing the text editor, enter the following code:
python
print('hello, world')
Note: Do not have any spaces before print
. Then, choose a directory, for example, C:\work
, and save the file as hello.py
. You can then open the command line window, change the current directory to where hello.py
is located, and run the program:
C:\work> python hello.py
hello, world
You can also save it with another name, such as first.py
, but it must end with .py
. Other extensions will not work. Additionally, the file name can only be a combination of English letters, numbers, and underscores.
If there is no hello.py
file in the current directory, running python hello.py
will result in an error:
C:\project> python hello.py
python: can't open file 'hello.py': [Errno 2] No such file or directory
The error means that hello.py
cannot be opened because the file does not exist. In this case, you need to check whether the file exists in the current directory. If hello.py
is located in another directory, you must first use the cd
command to change the current directory.
Running .py
Files Directly
Some people have asked if it is possible to run .py
files directly like .exe
files. On Windows, this is not possible, but on Mac and Linux, it is feasible by adding a special comment on the first line of the .py
file:
python
#!/usr/bin/env python3
print('hello, world')
Then, grant execute permission to hello.py
using the command:
bash
$ chmod a+x hello.py
Now you can run hello.py
directly. For example, on Mac:
bash
$ ./hello.py
hello, world
Summary
Write Python programs using a text editor and save them as
.py
files to run them directly with Python.Differences between Python Interactive Mode and Running
.py
Files:Entering
python
directly into the interactive mode starts the Python interpreter and waits for you to input source code line by line, executing each line as you enter it.Running a
.py
file directly starts the Python interpreter and executes all the source code in the file at once, without the opportunity to input code interactively.
When developing Python programs, you can write code in a text editor while simultaneously opening an interactive command window. During the coding process, you can copy parts of your code to the command line to verify them, making your workflow more efficient! The prerequisite is having a large 27-inch monitor!