Quick Start Scripted Running of Python Code: Sharing of Practical Skills!

bc6fe7ba78c64596441aa510832cff6d.jpeg

Author: CDA Teaching and Research Group

Python is a computer programming language. It is an object-oriented dynamic type language, originally designed for writing automation scripts (shell), with the continuous update of the version and the addition of new language features, it is increasingly used for the development of independent and large-scale projects.

What is a Python script

A script is essentially an executable text file, which requires a corresponding translation tool to interpret and execute it. The Python interpreter is such a program.

Two ways of running Python:

  • Interactive execution mode: execute line by line.

  • Write the code into a text file with a suffix of .py, execute it as a script, and execute all the code automatically.

  • For most data analysis scenarios, interactive code writing is more suitable, and tools such as ipython and jupyter notebook are more suitable. But for engineering, the code needs to be run automatically throughout the whole process until the termination condition is met. Such tasks are suitable for script running. For example, developed website programs, crawler programs, ETL programs, self-developed modules, etc.

How does Python execute

interactive mode

Execute Python in the interactive mode. In this mode, there is no need to create a script file, and you can directly write the corresponding Python statement in the interactive mode of the Python interpreter.

windows system

Find "Command Prompt" in the start menu, open it, and enter the command line mode.

309e2a4f1e9099cdf1c0cfc696731e67.png

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

d6a5cfb77f6f103dd7c91159cb510bff.png

Linux system

Enter python directly in the terminal. If python3 is installed, enter the corresponding version of the Python interactive environment according to the name of the soft link you built.

exit interactive mode

To exit the interactive mode, use the exit() function to complete the exit.

script mode

Write a script file through a text editor, and the script can be executed directly on the command line, such as CMD and powershell under the windows system, and bashshell under the Linux system. The first syntax for Python script execution is as follows:

python解释器 python脚本 参数1 参数2 参数3
  • The python interpreter indicates the python interpreter. If the path of the interpreter is not added to the environment variable path, you need to write the absolute path of the python interpreter.

  • The python script is the file where we write python code, which can be described by relative path or absolute path

  • Parameters: After the python script, spaces can be used to collect multiple parameters, which are used to set the execution of the program. In the program, use the args of the sys module to obtain parameters

Each of the above parts needs to be separated by adding a space

For example: there is a test.py file, put it on the desktop; the python interpreter is in the ProgramData\Anaconda3 folder of the d disk, you can write like this:

d:\ProgramData\Anaconda3\python C:\Users\EDZ\Desktop\test.py 12 34

c2b192d2b39b3b231e0bf9ddf518f197.png

Windows system

On the Windows system, some settings can also be made to realize the double-click mode to automatically run the script program. Methods as below:

1) Right click on the script, select open method, and select other applications

9a078bda426e8d353ceb7ede0d1d9016.png

2) If other applications do not have the program we need, you need to select more applications, and we can also check Always use this application to open .py files to set other python scripts to open in this way.

63de662492fc4747a14f8b090f43c887.png

3) If the listed program does not have a python interpreter, we need to go to the computer to select the location of the python interpreter to specify the program to open this file

ca41399975029e4909eccc8bcafabd16.png

For example, my python is installed based on anaconda, so its path is here:

8ed41390762f526c4f2cc10807a4f807.png

In this way, the python code can be run, and the next time the .py file can be directly double-clicked, it will be automatically executed by the python interpreter.

In this mode, there is no need to create a script file, and you can directly write the corresponding Python statement in the interactive mode of the Python interpreter.

How to write a Python script program

The general writing format of a python script is as follows:

#!/usr/bin/python
# 编写一些变量及一些函数或者定义一些类
def some_func():
    pass
......

if __name__=="__main__":
    pass

A simple Python script program, roughly written as shown above. Generally, functions, classes, data objects and other information are defined first, and then the execution logic body of the program is written in the following if statement (that is, class instantiation, function call, etc.).

Each python script has an __name__attribute. When the script is the main entry of the program, __name__the value is "__main__". When the script is used as a module by other script imports, the __name__attribute value of the script is the script file name, which is the module name. If you determine the usage scenario of the script, for example, as the main entrance of the program, you can write the code directly from top to bottom without following this mode.

61b51abb6c4ee4e71e2aff6a75bcb4a0.gif

a3737fbbeb012f378e6f311b4ad475b3.gif

c5812cbf9e587f75cea30fef049fd63d.jpeg

Supongo que te gusta

Origin blog.csdn.net/weixin_38754337/article/details/130143545
Recomendado
Clasificación