Python development tool pycharm quick start

 Click the link to join the QQ group 522720170 (free public classes, videos are available): https://jq.qq.com/?_wv=1027&k=5C08ATe

 

what this tutorial is about

This tutorial is designed to walk you through creating, running, and debugging a simple Python project, step-by-step, using PyCharm - a Python IDE with a complete set of productive development tools.

What this tutorial is not about

Python programming is beyond the scope of this tutorial. To learn more about the Python language, please refer to the official website.

before starting

please ensure:

§ You are using PyCharm version 2.7 or higher

§ At least one Python interpreter, version 2.4 to 3.3, is properly installed on your computer.

Download and install PyCharm

If you don't have PyCharm yet, download this page from here. To install PyCharm, follow the instructions, depending on your platform.

Start PyCharm

There are many ways to start the IDE, depending on your operating system.

§ If you have a desktop shortcut icon, just double-click it.

§ According to the bin directory of your PyCharm installation, double-click pycharm.exe or pycharm.bat (Windows), or pycharm.sh (MacOS and Linux).

PyCharm starts and displays the welcome screen:

Create a simple Python project in PyCharm

To create a new project, click the link Create New Project. You see, the Create New Project dialog, where you have to define all the new projects to make the necessary settings.

Note that you can create a new project anytime... In order to do this, use File → New Project on the main menu.

First, specify the project name - let it be MySimplePythonApplication. It is important to note that PyCharm displays the project location by default. You can accept the default location, or click the browse button to find some suitable location of your choice.

Next, select the project type. PyCharm presents several project templates for the development of different types of applications (Django, Google AppEngine, etc.). When PyCharm builds a new project from a project template, it generates the corresponding directory structure and specific files.

However, here our task is to create a project in Python. In this case, we choose an empty project of the type - it is most appropriate for pure Python programming. PyCharm does not generate any special files or directories in this case.

Finally, let's choose a Python interpreter. As you can see, PyCharm informs you that the Python interpreter has not been selected. Since you have at least one Python interpreter at your disposal, let's define it as the project's interpreter.

To do this, click the browse button next to it. In the Python Interpreters dialog, click, select Local..., then select the desired interpretation from the file system:

When all the necessary settings are done, the OK button becomes available - so click it and have your project ready.

Explore and configure project structure

You can view the initial project structure in the Project tool window:

As you can see, the project only contains the root of the project, and the Python interpreter you specified in the External Libraries node.

Next, let's explore and configure the project structure in detail: click on the main toolbar and select the Project Structure page:

Under the project's root directory, you see the .idea directory - it contains the MySimplePythonApplication.iml file, reflecting the project structure, and several XML files, each responsible for its own set of settings, which can be identified by their name: encodings .xml, .xml for VCS, etc. It should be noted that the .idea directory is not visible in the Project tool window.

Next, let's add the source root where all the work will actually be performed. On the same Project Structure page, right-click the project root directory and select New Folder on the context menu:

Next, enter the directory name:

Finally, let's celebrate this directory as the source root: select the src directory, click on the src directory you see, now marked with

icon. Click OK to apply the changes and close the Settings/Preferences item dialog.

Note that this step is actually optional. You can create a file in the project root and it will be considered source, because by default the project root is the source root.

Life is not easy, you have to work very hard
In the enrollment of the May class of Xiaoqiang performance test, many small partners have already signed up. All students enjoy free, unlimited learning opportunities at no additional cost
Interested private chat QQ (no reply in the group): 2083503238, 1684129674, 480934277 (please do not repeat the consultation)
If you have no questions, you can ask me to pay for the registration (support Alipay Huabei, credit card, bank card transfer)
Some students' employment situation display: http://www.xqtesting.com/index.php/blog/offer-31.html

Create a Python class

Select the SRC directory in the Project tool window and press ALT+INSERT:

Select the option Python file, from the popup window, and type the new filename (solve):

PyCharm creates a new Python file and opens it for editing:

Edit source code

Let's first take a look at the Python file we just generated. The stub contains only two lines:

_author_ = 'wombat'

_project_ = 'MySimplePythonApplication'

Because a Python file is being generated as a template file, PyCharm has been superseded by the actual values ​​instead of the predefined variables $PROJECT_NAME and $USER.

Now let's move on to making something meaningful - a simple application that solves a quadratic equation.

Immediately for you to start typing, you understand that PyCharm is a nifty choice for programmers. For example, you want to create a Python class. When you just start typing keywords, a list of suggestions displays:

Select the class for the keyword and type the class name (Solver). PyCharm immediately informs you that the colon is missing, then expects indentation:

Note the wrong stripe in the gutter on the right. Place the mouse pointer over a wrong stripe, and PyCharm shows a balloon with a detailed explanation. As PyCharm analyzes your instant code, the results are displayed immediately in the check indicator above the gutter on the right. The indication of this check is like a traffic light: when it is green, everything is ok and you can continue with your code; a yellow light means some glitches but won't affect the compilation; however, when the light is red, This means you have something seriously wrong.

Let's go ahead and create the function "demo": when you only need to enter the opening parenthesis, PyCharm creates the entire code construct (required parameter "self", closing parenthesis and colon), and provides proper indentation:

Note: Unused symbols are grayed out as you type:

Once you've calculated the discriminants, they appear as usual. Second, be aware of the unresolved reference to "math". PyCharm emphasizes it with a red curved line, and represents a red light bulb.

Let's make a brief postscript into PyCharm's intended actions and the concept of quick fixes. When you write your code, it should sometimes modify the code structure - in this case PyCharm displays a yellow light bulb. However, if PyCharm encounters an error, a red light bulb is displayed.

In both cases, to see what PyCharm recommends you do, press Alt+Enter - this will bring up a list of suggestions, which in our case contains several possible solutions:

Let's choose the math library to import. Import statements are added to the Solver.py file. Next, compute the roots of the quadratic equation and print them out, and finally, let's call this function to demonstrate a solution like this:

import math

_author_ = ‘Mads Spiral’

_project_ = 'MySimplePythonApplication'

class Solver:

def demo(self):

a = int(input("a "))

b = int(input("b "))

c = int(input("c "))

d = b ** 2 - 4 * a * c

disc = math.sqrt(d)

root1 = (-b + disc) / (2 * a)

root2 = (-b - disc) / (2 * a)

print(root1, root2)

Solver().demo()

Then press Ctrl + Shift + F10 to run the script. The console will be displayed in the Run tool window. In this console, you must enter the A, B and C values ​​and expect to see the results.

Oops... PyCharm reports a runtime error:

It seems that some analysis is desirable, so let's make sure that 'D' in the square is non-negative, and report an error when it's negative. To do this, select the discriminative computation statement and press Ctrl+Alt+T (Code→Surround with):

PyCharm creates a stub 'if' structure that lets you populate its tasks with the correct content. In the end, it would be nice to repeat the entire calculation more than once, so let's use the "Surround with" action again: select the demo of the entire body of the function, and wrap it around it. You will end up with code like the following:

import math

_author_ = ' Mads Spiral'

_project_ = 'MySimplePythonApplication'

class Solver:

def demo(self):

a = int(input("a "))

b = int(input("b "))

c = int(input("c "))

d = b ** 2 - 4 * a * c

if d>=0:

disc = math.sqrt(d)

root1 = (-b + disc) / (2 * a)

root2 = (-b - disc) / (2 * a)

print(root1, root2)

else:

print("error")

Solver().demo()

Next, let's run and debug the script.

running application

You've already started Solverscript's script, so we'll just remind it how to do it. PyCharm recommends several ways to run scripts that are opened in an editor.

§ First, you can use the keyboard shortcut Ctrl + Shift + F10

§ Second, you can use context menu commands, invoked by right-clicking on the editor background:

§ 最后,也可以从主工具栏运行脚本,使用临时运行/调试配置“Solve”(运行/调试配置的概念将被更详细地考虑在下一节):

在任一情况下,PyCharm打开运行工具窗口,并显示了应用程序的输入和输出:

运行/调试配置

每个脚本使用的是特殊的配置文件,或者执行运行/调试配置。这样的一个配置文件同时用于运行和调试应用程序,并指定脚本的名称,工作目录,行动启动等之前执行

PyCharm提出了一些默认的运行/调试配置,为不同类型的应用程序(Python脚本,Django的应用程序,测试等),您可以查看可用默认的运行/调试配置对话框,它被调用或者通过运行→编辑配置...命令,在主菜单中,或通过单击主工具栏的运行区域的下拉列表:

让我们来看看Edit Configurations。其左手部分包含一个树状视图有两个顶级节点:Python的和默认值:

较低的节点包含的默认运行/调试配置列表中。这些默认运行/调试配置是无名的,但每一个新的运行/调试配置在默认的理由创建的,并得到您所选择的名称。

上级节点被称为的Python和只包含一个运行/调试配置求解器,其以灰色显示。这是什么意思?

运行/调试配置Solver是一个临时配置文件,其中PyCharm产生了,当你刚刚运行求解器脚本。它所驻留的节点下的Python,由于该运行/调试配置在底座上创建Python类型的默认配置。

您可以保存本次运行/调试配置,从而使其永久化。永久运行/调试配置呈现在一个正常的字体。与临时配置,永久那些数量是无限的。

让我们使用相同的临时运行/调试配置求解器进行调试的求解器脚本。

调试应用程序

你会做什么来执行一步你的应用程序的步骤,检查相关的变量,手表,或线程程序的信息,找出异常的起源?这是在调试过程来帮助。

要开始调试,你必须设置断点第一。要创建一个断点,只需点击左侧装订线:

接下来,用鼠标右键单击编辑器的背景,并选择调试“Solver”上下文菜单:

PyCharm启动调试会话,并给出了调试工具窗口。下图对应窗格和选项卡的默认布局:

在调试工具窗口显示了框架,变量和手表,以及控制台,其中显示所有输入和输出信息的专用窗格。如果你想在控制台总是可见的,只需拖动到所需的地方:

使用步进工具栏按钮来逐步执行应用程序:

当你通过申请步骤,每一个到达断点变成蓝色:

探索导航

导航提供了一个特殊的热情来PyCharm。让我们来简单介绍一下在刚刚有些众多PyCharm的导航设施。

§ 想象一下,你已停止工作,出去喝杯咖啡......当你回来,你不记得究竟你一直在做和你去哪儿停止。在这种情况下,你可以使用的最需要的功能之一-浏览到最后编辑的位置。按Ctrl + Shift + Backspace键 -在这里你!

§ 随着PyCharm,它很容易去一个符号的声明。例如,在我们的例子中,将插入符号的调用平方根函数,然后按Ctrl + B键 - PyCharm立即打开math.py在声明开方功能:

§ 非常有用的是迅速找到任何类,文件或符号的名字的能力。例如,按Ctrl + Alt + Shift + N键,然后输入你想要去到一个符号的名称:

你可以找到所有下可用的导航命令导航菜单。另外,在本教程只是几个例子

重构

让我们来改变函数的名称演示,并给它一些更具描述性的名称,例如,计算。这很可能只是为了改写现有名称与一个新的。然而,在这种情况下,你将不得不输入新名称的两倍:对于函数调用的第一个时间函数的声明,和第二时间。在这个小例子就不是一个问题,而是考虑在一个大型项目中,有许多函数调用......这是更建议使用重命名重构代替。

将插入符号在函数声明时,按Shift + F6键,然后键入新名称,在重命名对话框:

点击重构。所有发现的事件出现在查找工具窗口:

点击做重构按钮-你看到的函数名称已更改为函数声明和函数调用两种:

这是可能的,以进一步修改这个类:它移动到不同的文件夹,更改签名计算功能,所有这些行动都是由各种重构手段进行提取变量等。我们将在专门的教程考虑这些重构的更多细节。

Guess you like

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