Three ways to execute Python scripts

In Python interactive mode, you can directly execute the corresponding script without Python interpreter. 1.
1) How to open interactive mode:
Under Windows:
Find "command prompt" in the start menu and open it to enter the command line mode:


Enter Python in the command line mode to enter the interactive mode of Python

Under Linux:
Directly input Python in the terminal. If Python 3 is installed, enter the corresponding version of Python interactive environment according to the name of the soft connection I built. For example, I use Python 3 to establish the soft connection, which is input into Python 3.

2) Exit the interactive mode and directly enter exit().
Under Windows:

Under Linux:

3) Output in interactive mode: Hello World!
Windows:

Linux:

[Vicky@localhost code]$ touch hello.py
[Vicky@localhost code]$ vi hello.py 
[Vicky@localhost code]$ python3 hello.py 
Hello World!

In this way, pay attention to the path where the script file is located. If the current working path and the script file are not in the same path, enter the path where the script file is located or give the full path of the script file.
1) Enter the path where the script file is located and execute it

C:\Windows\System32>G:
G:\test>python hello.py
Hello World!

2) Give the full path of the script file

C:\Windows\System32>python G:\test\hello.py
Hello World!

3. Specify the path where the python program is located in the script file, modify the file to an executable file, and then run the file directly
Under Linux:
1) Modify the file and add #/ usr/bin/python3

[Vicky@localhost code]$ vi hello.py 
[Vicky@localhost code]$ cat hello.py 
#!/usr/bin/python3
print("Hello World!")

2) Modify file permissions and add executable permissions

[Vicky@localhost code]$ chmod u+x hello.py 
[Vicky@localhost code]$ ls -la hello.py 
-rwxrw-r--. 1 Vicky Vicky 41 10 September 19-15:40 hello.py

3) Run

[Vicky@localhost code]$ ./hello.py 
Hello World!

When executing in this way, the interpreter must be specified in the script file, otherwise the script file cannot be run directly

[Vicky@localhost code]$ cat hello.py 
print("Hello World!")
[Vicky@localhost code]$ ls -la hello.py 
-rwxrw-r--. 1 Vicky Vicky 22 10 September 19-15:40 hello.py
[Vicky@localhost code]$ ./hello.py 
./hello.py:Line 1: Unexpected symbol `"Hello World!"' Syntax error nearby
./hello.py:Line 1: `print("Hello World!")'

4. Comparison between interaction mode and script file mode
1) In interactive mode, the calculation results will be printed automatically, but not through script files
Interactive mode:

[fanya@localhost code]$ python3
Python 3.6.5 (default, Oct 19 2018, 10:46:59) 
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 100+200
300
>>> exit()

Script file:

[fanya@localhost code]$ vi cal.py 
[fanya@localhost code]$ cat cal.py 
100+200
[fanya@localhost code]$ python3 cal.py 
[fanya@localhost code]$ 

It can be seen that there is no output. At this time, if you want to output, you must use the print function to print.

[fanya@localhost code]$ vi cal.py 
[fanya@localhost code]$ cat cal.py 
print(100+200)
[fanya@localhost code]$ python3 cal.py 
300
[fanya@localhost code]$ 

2) In the interactive mode, each input statement will not be saved and will disappear after exiting the interactive environment, but we can save all the statements we have written through the script file. Therefore, Python code is usually written by writing script files.
Note: when writing script files, do not use the notebooks brought by word and windows, because they will be saved in utf-8 BOM format, which will lead to script execution errors. You can use sublime, editplus, notepad++

Related resources

http://www.dcsdn.com/doc/do-3728
http://www.dcsdn.com/doc/do-3662
http://www.dcsdn.com/doc/d-3573

Original link: https://blog.csdn.net/lijing_2011/article/details/83184067

Keywords: Python Linux Pycharm

Added by Luvac Zantor on Thu, 10 Mar 2022 10:32:38 +0200