1, themes,

Explains how to create, run, and debug an application using Pycharm.

2. Preparation

Pycharm version 2.7 or higher.

Install at least one Python interpreter, 2.4 through 3.3

3. Download and install Pycharm

Download address: this page

Start PyCharm

Double-click the shortcut (Pycharm.exe or Pycharm.bat in Windows; MacOS and Linux for Pycharm. Sh), Welcome screen:

Create a simple project

Click the Create New Project link to enter the Create Project dialog box and set the Project.

Of course, you can also create a New Project at any time by using the main menu command File → New Project:

First of all, as the project name, named MySimplePythonApplication here. Then change the project location, either using the default location or specifying it by clicking the Browse button.

Next, choose the project type. Pycharm presets several types of templates (Django, Google AppEngine, etc.) and creates the related files by default.

Here we choose the Empty project type (more suitable for simple Python projects) and do not require Pycharm to default any files.

Finally, specify the Python interpreter, selecting it from the drop – down list.

Click OK and the project is created.

6. Browse the project directory structure

The initial Project directory (in Project Tool Window) looks like this:

At this point, only the project root directory and the External Libraries directory that defines the Python interpreter exist.

Click the button in the main toolbar and select the Project Structure page to view the detailed Project directory information:

In engineering idea directory under the root directory to store the MySimplePythonApplication. On iml file, used to record the current engineering structures; There are also several XML files in the directory that hold configuration information. The IDEA directory is not visible in the Project Tool Window.

Next add the working directory to the root directory. On the Project Structure page, right-click the Project root directory and select New Folder:

Enter a directory name:

Finally, mark the directory to the source file root: select the SRC directory and click.

Click OK to close the Settings dialog box.

Of course, there is no unique way to add directories. You can also create Python files directly in the project root directory, which is the source root by default.

Create a Python class

Select the SRC directory in the Project Tool window and press Alt+Insert:

Select Python File and enter the name (Solver) :

Class created, open edit:

Edit the source code

First, the file has two lines of code generated by default:

This is generated by Pycharm from the file Template and automatically replaces the form variables PROJECTNAME and PROJECT_NAME and PROJECTNAME and USER.

Next we write a small program to solve the quadratic equation.

Pycharm provides a variety of tips to help you with your coding. For example, when creating a class, just enter the keyword and a list of tips will pop up:

Select the keyword class and enter the class name (Solver). Pycharm will prompt you to continue typing:

In this case, an error mark is displayed in the right slot, and an error message is displayed when the mouse hovers over the right slot. The indicator light at the top of the right slot marks the current code check body, green for everything is ok, yellow for warning, and red for error.

Continue creating the “demo” member function and experience the auto-completion of the Pycharm code:

Continue typing, unused variables are shown in gray:

The discriminant is then computed using the Math module, but Pycharm reports an error (red wavy line and red light bulb) because it has not been imported.

See intention actions and quick fixes for the use of red bulbs.

Press Alt+Enter to see the prompts given by Pycharm:

Select import the Math library, and then the demo function in the Solver class to compute the discriminant:

Press Ctrl+Shift+F10 to run the script file, a console appears, enter the values of A, B, and C, and Pycharm encountered an error:

Here is an error when d (discriminant) is negative. To avoid this, Surround with Ctrl+Alt+T:

Pycharm automatically creates an if statement structure. Finally, if you want to execute the program multiple times, you need to nest a while loop around it. The result is as follows:

Next, prepare for debugging.

9. Run the program

There are three ways to run a script file:

(1) Ctrl+Shift+F10 shortcut key

(2) Use the shortcut menu options

  

(3) Use the run button of the main menu

View the run result:

10. Run/debug the configuration

Each script file is executed according to the specified configuration file (Run /debug Configuration), including the script name, working directory, preprocessing, and so on.

Pycharm already preset a number of common configuration file types (for Python Scripts, Django Applications, Tests, etc.), which can be viewed in the Run/Debug Configurations Dialog. You can do this by running →Edit Configurations… Command or click on the Run drop-down list in the main toolbar to open this dialog:

Look in detail at the Edit Configurations dialog box, which contains two main sections: Python and Default

The contents in the default run/debug list are the default configuration information. They do not have specific names, but are automatically loaded and used based on the type.

The top node named Python contains only the configuration file Solver shown in gray. It is a temporary profile that is the default configuration of the Python type created by Pycharm.

You can permanently save your configuration files as many times as you want.

Debug the program

To set breakpoints before debugging, click the left slot:

Then right click on the edit area and select Debug ‘Solver’ :

The Debug Tool Window is displayed, and the debugging starts. The default layout of the debugging window is as follows:

Here shows the frame, variable, and control lamp. Of course, if you want the console to be visible at all times, drag it to the specified area:

Step debugging using Stepping Toolbar Buttons:

If a breakpoint is hit, the line becomes blue:

12. Preliminary study on navigation function

If you break in the middle of programming and come back unsure where to start, this uses an important navigation feature: jump to the last edit. Press Ctrl+Shift+Backspace.

A quick look at the symbol definition, such as placing the cursor on the SQRT call and pressing Ctrl+B will redirect Pycharm to the specified definition of math.py:

Quickly find symbols, classes, files. Press Ctrl+Alt+Shift+N and enter a name:

See here for more details.

13. Code refactoring

If you change the name of a function, demo, theoretically all calls to it need to be changed. Pycharm provides code refactoring here.

Press Shift+F6 and enter a new name in the dialog:

Click Refactor to display the results in the Find Tool window:

Click the Do Refacto button to complete the substitution:

Of course, we can make more changes, such as moving the file location, changing the parameter structure of the function, extracting variables, and so on. These are all kinds of refactorings. We’ll cover this in more detail in a future tutorial.

This post is from SDK Community (www.sdk.cn/).