Python development environment construction and preparatory knowledge

Special Note

If the reader has built a Python development environment, you can skip the first part of this chapter, but it should be noted that some of the example codes listed in this course have Chinese comments, and some integrated development environments may not support Chinese comments. Take care to remove the comment. In addition, if readers feel that it is cumbersome to build a development environment, they can use the IDLE that comes with Python as the development environment. For the installation method, please visit: "Python3 Getting Started Notes" https://www.cnblogs.com/weven/p/7252917.html

Python development environment setup

There are many IDEs for developing Python. This article introduces the method of building a development environment based on Eclipse+PyDev+Python.

  • Introduction to Eclipse

Eclipse is a Java-based extensible development platform. Its official download includes J2EE direction version, Java direction version, C/C++ direction version, mobile application direction version and many other versions. In addition, Eclipse can also develop languages ​​such as Python, Android, and PHP by installing plug-ins. What this article will introduce is to use the Eclipse and PyDev plug-ins to install the Python development environment.

  • environment

OS : Windows 7 

Python:3.6.2

Java:8u31 

Win7 32-bit, Mac OS operating system is the same as the following installation method.

software download

Eclipse download

We can enter the Eclipse official website download interface to download the Eclipse software. On this page, you can see Eclipse versions for different development needs. This article uses the Eclipse IDE for Java and DSL Developers. Currently, the latest version is Eclipse Oxygen.2 (4.7.2) Release, which was released in October 2017. In addition, it should be noted that you need to choose the correct system bits (32/64bits) according to your operating system. 

PyDev offline download

We can see some valuable information on the PyDev project download page : 

  1. Eclipse, Java, PyDev version correspondence;
  2. The URL for Eclipse to install PyDev online;
  3. Offline installation of PyDev download address (Get zip releases), click to enter the download page of SourceForge. This article describes how to download offline.

enter image description here

Eclipse installation

It should be noted here that the Eclipse installation requires a Java environment. If the Java environment has not been installed, please download and install the JDK ( click here ).

Eclipse is actually a software based on the OSGI framework. It does not need to be installed on Windows systems. It only needs to be decompressed and double-clicked to open eclipse.exe. On Mac OS, it is different, you need to double-click the .dmg file to install. On the first run, you will be asked to enter the working path, you can customize it or accept the default path.

PyDev plugin installation

There are two ways to install the Eclipse plug-in: offline and online. This article introduces the online installation method.

Open Eclipse and select "Help" -> "Install New Software". In the pop-up dialog box, click the Add button to add a new installation source, as shown in the following figure.

enter image description here

Fill in the URL of the installation source at Location: http://www.pydev.org/updates/ (many blogs write http://pydev.org/updates), this address is not static, so it is best to go to the official website Making sure. The confirmation method is described in the "Software Download" section above.

In addition, you need to take a name to fill in the Name, for example, I wrote PyDev here. Uncheck "connect all update sites during install to find required software", otherwise, when installing a new plug-in, all possible update sites will be searched online, resulting in unpredictable installation time and possible installation failure. After confirming, you can see a Pending process, and then get the plug-in as shown in the following figure. Generally speaking, we only need to select PyDev. Here I have installed both (not recommended):

enter image description here

After checking, click Next to install. However, due to network reasons, this method of installing PyDev is very likely to fail, prompting a network connection error, etc.

Python installation

The installation of Python is relatively simple, go to the Python official website to download the installation package. After entering the official website, the official website will recommend the version of the Python installation package according to your computer operating system. As shown in the figure below, you can download the latest installation package according to the recommendation. It should be noted that Python currently has two major versions, Python2 and Python3. There are significant differences. This course is written based on Python3. Therefore, readers are requested to choose the Python3.X installation package. For the specific installation steps, please refer to the blog post "Python3 Getting Started Notes - Windows Installation and Operation" . 

enter image description here

PyDev plugin configuration

After the PyDev plugin is installed, it cannot be used normally, and the Python interpreter needs to be configured.

Open Eclipse, select "Window" -> "Preferences" (if it is a Mac, press Command and keys at the same time to call up Preference), find "PyDev", and select "Interpreter" -> "Python" in it. Click "New" to add a path to the existing Python interpreter in the system (determined according to the path installed by yourself). After it is determined, it will be processed briefly to get its Libraries, Buildins, etc. Of course, you can also perform some other configurations on PyDev according to your own programming habits, which will not be discussed here.  

enter image description here

Create a Python project

The Python development environment has been configured before. Let's create a new project to test it, and it can indeed run.

Click "File" -> "New" -> "Other", find "PyDev", select "PyDev Project", and click "Next". Take a project name, such as helloPython, in addition, you can also choose the language version and interpreter version of Python, as shown in the following figure:

enter image description here

Click "Finish" to complete the project creation. You will then enter the PyDev view for Python development. Here, we will write the simplest program for testing. Right-click the src directory of the project, select "New" -> "PyDev Package", and create a Python package, which is also named helloPython here. Then right-click the package, "New" -> "PyDev Module", which is also named helloPython here. Double-click to open helloPython.py and add the following code.

if __name__ == '__main__': 
    print("hello world!")  

Right-click the project, select "Run As" -> "Python Run", or Ctrl+F11 to run the project. At this point, you can see the running result of the project in the Console window below: hello world!.

Python prerequisites

Programming language

A programming language is a formal language used to define computer programs. It is a standardized communication technique used to give instructions to computers. Computer languages ​​allow programmers to define exactly what data a computer needs to use and exactly what action to take in different situations.

explain

The above definition is rather obscure to read, so let me explain it in a simple way.

In the history of human development, the appearance of writing is generally regarded as a sign of civilization. Whether it is Chinese, English or French, they are all tools for human communication. Words make communication and exchanges between human beings have rules to follow, which greatly improves the efficiency of information dissemination. Since the birth of the computer, people have always hoped to give the computer the characteristics of language, so that the computer can communicate like a human, and the programming language can be regarded as a tool for "communication" between humans and computers, which enables the communication between humans and computers. Efficient communication is achieved.

high level programming language

In practice, people realize that it is quite difficult for people and computers to "communicate" directly. The language that computers can directly understand is machine code composed of 0 and 1, and this machine code does not conform to human cognitive habits. Therefore, advanced programming Language came into being.

What is advanced? It refers to the understanding habits and way of thinking of human beings, away from the bottom layer of the computer. High-level programming languages ​​(such as Java, Python) enable people to express in a relatively easy way the instructions that the computer is expected to execute. However, the "articles" written in this high-level language cannot be directly understood by the computer. Therefore, a "translation" is also needed to translate the high-level language written by people into machine language that the computer can understand according to the rules. Depending on the programming language, there are also significant differences in "translation" here. A brief schematic diagram of the "communication" between humans and computers is as follows:

enter image description here

Significantly different from Java and C++

Readers who have studied C/C++ and Java must still remember the method of defining variables. In the following example, four variables a, b, c, and d are defined, and their numerical types are int, double, long, and float. When defining variables, We need to "explicitly" declare the type of the variable in order to open up space for the variable in memory. The memory space occupied by different data types is different, so it must be declared.

int a;
double b;
long c;
float d;

Unlike C++ and Java, variables in Python have no type, let alone declare variable types. Variables are just references to objects, while all types in Python are objects, including functions, modules, numbers, strings, lists, tuples, dictionaries, etc.

As shown below, the variable a is defined and assigned. The same variable is assigned three different types in the same program. The reason why this can be done is because the variable has no type, it can point to any type. From this point of view, The differences between Python and C++ and Java are obvious.

# -*- coding: UTF-8 -*

a = 12
print("a=", a)
a = "ABCDE"
print("a=", a)
a = 1 + 4j
print("a=", a)

Execution result:  a= 12 a= ABCDE a= 1+4j

Description of this course example

In order to facilitate the reader's better understanding, this course lists many program examples, some of which involve Chinese comments. In addition, in order to visually display the results of program execution, the examples in the course use a lot of print statements to print the results to the console, which is convenient for Reader observation.

Regarding the example style of this course, here is a simple example:

# -*- coding: UTF-8 -*

#自定义函数,求两个数的和
def add(a, b):
    return a + b;

#直接调用内建函数pow(x,y),计算x的y次方
print('10的3次方 = %d' %pow(10,3))
print('1 + 2 = %d' %add(1,2))

执行结果:
103次方 = 1000
1 + 2 = 3
Special Note

In the above example, there are two points that need to be explained.

  • # -*- coding: UTF-8 -*

Python uses ASCII as the encoding method by default. If you include Chinese (or other non-English languages) in your Python source code, even if you save the Python source file you wrote in UTF-8 format, it will still run at runtime. May report an error: SyntaxError: Non-ASCII character '\xe5' in file XXX.py on line YY, but no encoding declared;

Solution: Add the following statement to the first line of each Python script file that contains comments in Chinese or other languages:

# -*- coding: UTF-8 -*-
  • #

"#" means a comment. When you enter it in the code, everything to the right of it will be ignored. Comments are extremely useful, not only helping others understand their code, but also prompting themselves.

Run a Python script via command

In the above example, we run the Python script through the Eclipse IDE (Integrated Development Environment). In fact, there are many ways to run the program. For the Python language, we can run it directly by command.

Windows system :

Enter the path where the Python script test.py is located, and type the command: Python test.py.

Note: The first letter of Python is capitalized.

Linux or Unix systems:

Enter the path where the Python script test.py is located, and type the command: python test.py.

Note: The first letter of Python is lowercase.


From: Ying Shulan's "Python Quick Start Practical Tutorial", only for personal learning records. For more detailed knowledge, please go to Gitbook to support Teacher Ying's original course!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324720367&siteId=291194637