PyCharm installation tutorial and usage tutorial (2020 latest version)

PyCharm is a Python IDE that can help programmers save time and improve productivity. So how do you use it? This article introduces PyCharm installation to plug-ins, external tools, professional version functions, etc., hoping to help everyone.

The Heart of the Machine has not introduced PyCharm systematically before. How to configure the environment, how to DeBug, how to synchronize GitHub, etc. may be learned through experience or exploration. In this article, we will not provide a very complete guide, but will introduce some of the most important capabilities of PyCharm. After understanding these, we need to learn more in practice later.

Readers of the machine heart should know PyCharm developed by JetBrains very well, which is almost the most commonly used IDE for Python. PyCharm can save us a lot of time, it can manage the code, and complete a lot of other tasks, such as debugging and visualization.

This article will introduce:

  • PyCharm installation

  • Write code in PyCharm

  • Run the code in PyCharm

  • Code debugging and testing in PyCharm

  • Edit existing projects in PyCharm

  • Search and navigate in PyCharm

  • Use version control in PyCharm

  • Use plugins and external tools in PyCharm

  • Use PyCharm Professional features, such as Django support and scientific mode

This article assumes that the reader is familiar with Python development and a certain version of Python is already installed on the computer. This tutorial will use Python 3.6 version, screenshots and demos are from macOS system. Since PyCharm can run on all major platforms, readers will see slightly different UI elements in other systems, and some commands may need to be adjusted.

PyCharm installation

This article will use the PyCharm Community Edition 2019.1 version, which is free and available on all major platforms. Only the last part of "PyCharm Professional Features" uses PyCharm Professional Edition 2019.1 version.

It is recommended to use JetBrains Toolbox App to install PyCharm. Using this App, you can install different JetBrains products or different versions of the same product, and update, roll back and easily delete any tools if necessary. You can also quickly open any project in the appropriate IDE and version.

Toolbox App installation guide, please refer to JetBrains official document: https://www.jetbrains.com/help/pycharm/installation-guide.html#toolbox.

The App will provide appropriate installation instructions based on your operating system. If it cannot accurately identify the system, you can find the appropriate system in the drop-down list in the upper right corner.

After the installation is successful, start the app and accept the user agreement. Under the Tools option, you can see a list of available products. Find PyCharm Community and click Install.

Alright, now PyCharm is installed on your machine. If you don't want to use the Toolbox app, you can install PyCharm separately.

Start PyCharm, and you will see the import settings pop-up window. PyCharm will automatically detect that this is the first installation and select the "Do not import settings" option for you. Click OK, then PyCharm will let you select the keymap scheme. Keep the default settings, click "Next: UI Themes" in the lower right corner:

PyCharm will ask to choose dark mode Darcula or light mode. You can choose your favorite mode and click "Next: Launcher Script":

This tutorial will use the dark mode Darcula.

On the next page, just keep the default settings and click "Next: Featured plugins", then PyCharm will display a list of available plugins. Click "Start using PyCharm", now you can write code!

Write code in PyCharm

In PyCharm, you can perform any operation in the "project". Therefore, first you need to create a project.

After installing and opening PyCharm, you will see the welcome page. Click "Create New Project" and a "New Project" pop-up window appears:

Specify the project location, open the Project Interpreter list, and choose to create a new project interpreter or use an existing interpreter. Select "New environment using", open the drop-down list on the right, and select Virtualenv, Pipenv or Conda. These tools can create separate Python environments for different projects, so as to save the dependencies required by different projects.

You can choose any of them, this tutorial uses Virtualenv. After selection, specify the environment location, and select the base interpreter to be installed in the system from the list of Python interpreters. Normally, just keep the default settings. There are two optional boxes below: inherit the global package environment in the new environment, make all other items available in the current environment, and do not select both.

Click "Create" in the lower right corner to create a new project:

A "Tip of the Day" pop-up window appears on the screen, and PyCharm provides tricks through the pop-up window every time it is started. Close the pop-up window.

Now we can start a new Python program. If you are using a Mac system, use the Cmd+N key; if you are using a Windows or Linux system, use the Alt+Ins key. Then select Python File. You can also select File → New in the menu. Name the new file guess_game.py and click OK. You will see the following PyCharm window:

As for the test code, let’s quickly write a simple guessing game, that is, the program selects a number for the user to guess. At each guess, the program will tell the user whether the number he guessed is larger or smaller than the mysterious number. When the user guesses the number game over. The following is the code of the game:

Type the above code directly instead of copying and pasting. You will see the following screen:

As shown in the figure above, PyCharm provides Intelligent Coding Assistance function, which can perform code completion, code inspection, error highlighting and quick repair suggestions. For example, type main and click the tab key, PyCharm will automatically complete the entire main clause.

In addition, if you forget to type if before the conditional sentence, add .if at the end of the sentence and click the Tab key, PyCharm will repair the if conditional sentence. This usage also applies to True.while. This is the Postfix Completion function of PyCharm, which can help users reduce the number of times the backspace key is used.

Run the code in PyCharm

Now you have coded the game and can run it.

The game program has three operating modes:

  1. Use the shortcut key Ctrl+Shift+R in the Mac system, and use the shortcut key Ctrl+Shift+F10 in the Windows or Linux system.

  2. Right-click on the background and select "Run "guess_game"" from the menu.

  3. Since the program has a __main__ clause, you can click the small green arrow on the left side of the __main__ clause and select "Run 『guess_game』".

Use any of the above methods to run the program, and a Terminal pane will appear at the bottom of the window, showing the output of your code:

You can play this game and see if you can guess the numbers. (Professional advice: start guessing from 50.)

Code debugging in PyCharm

Have you found the mysterious number? If you find it, you may see something strange: the program did not print out the congratulatory message and display the exit button, but restarted. This is where the bug is. To find out why the program restarts, you need to debug.

First, click on the blank area to the left of the 8th line of code to set a breakpoint:

The breakpoint means that the program will stop automatically when it reaches this line. You can explore what is wrong with the code after the breakpoint. Next, choose one of the following three ways to start debugging:

  1. Use the Ctrl+Shift+D keys in Mac systems, and Shift+Alt+F9 keys in Windows or Linux systems.

  2. Right-click on the background and select "Debug 『guess_game』".

  3. Click the small green arrow on the left side of the __main__ clause and select "Debug 『guess_game』".

After that, you will see the Debug window appear at the bottom:

Follow the steps below to execute the program debug:

  1. Note that the current line is highlighted in blue.

  2. The Debug window displays random_int and its value. Record this number. (The number in the picture above is 85.)

  3. Click F8 to execute the current line of code and execute to the next line of code. If necessary, you can also use F7 to jump to the function in the current line. As you continue to execute the statement, the change of the variable will automatically appear in the Debugger window.

  4. Note that there is a Console label to the right of the Debugger label. The Console label and the Debugger label are independent of each other. You can interact with the program in the Console and execute debug actions in the Debugger.

  5. Go to the Console tab and enter the guessing process.

  6. Type the number displayed in the Debugger label on the left, and click Enter.

  7. Switch back to the Debugger tab.

  8. Click F8 again to calculate the if statement. Note that you are now on line 14. Why not line 11? Because the if statement on line 10 is evaluated as False. So why does it count as False after you type in a number?

  9. Look carefully at line 10 and notice that we are comparing user_guess with a wrong item. We should compare the number guessed by the user with random_int, but here we compare randint (a function imported from the random package).

  10. Change randint to random_int, follow the same steps to restart debugging. You will find that this time line 11 is reached, and line 10 is calculated as True:

Congratulations, the bug has been fixed!

Code testing in PyCharm

Applications that are not unit tested are not reliable. PyCharm can help you write unit tests and run them quickly and comfortably. By default, unittest is used as a test runner, and PyCharm also supports other test frameworks such as pytest, nose, doctest, tox, and trial. For example, you can choose the pytest test runner for your project as follows:

  1. Open the Settings/Preferences → Tools → Python Integrated Tools setting dialog box.

  2. Select pytest in the Default test runner field.

  3. Click OK to save the setting.

The examples in this tutorial will use the default test runner unittest.

In the same project, create the file calculator.py, and put the following Calculator class into the file:

PyCharm makes it easy to create tests for existing code. Open the calculator.py file and perform any of the following steps:

  • Use the Shift+Cmd+T key in Mac system and Ctrl+Shift+T in Windows or Linux system.

  • Right-click the background of this category and select "Go To and Test".

  • In the main menu, select Navigate → Test.

Select "Create New Test..." to get the following window:

Keep the default settings for Target directory, Test file name and Test class name. Select the two methods to be tested in the above figure and click OK. All right! PyCharm automatically created the file test_calculator.py, and created the following stub test in it:

Run the test using any of the following methods:

  • Use the Ctrl+R key in Mac system, and Shift+F10 key in Windows or Linux system.

  • Right-click on the background and select "Run "Unittests for test_calculator.py"".

  • Click the small green arrow to the left of the test class name, and select "Run 『Unittests for test_calculator.py』".

You will see a test window appear at the bottom, all tests have failed:

Note that the left side is the hierarchical structure of the test results, and the right side is the output of the terminal. Now, change the code to the following code to implement test_add:

Re-run the test and you will see that one test passes and the other fails. Follow the steps below to explore different options to show passed and ignored tests, sort tests alphabetically, and sort tests by duration:

Note that the function of the sleep(0.1) method in the above figure is to slow down one of the tests so that the tests can be sorted by duration.

Edit existing projects in PyCharm

Single-file projects are great examples, but you usually need to deal with larger projects. This part will introduce how to use PyCharm to handle larger projects.

To explore the project-centric features of PyCharm, you will use the Alcazar web framework (which is used for learning purposes). Copy the repo locally (Address: https://realpython.com/optins/view/alcazar-web-framework/).

When you have a project locally, use any of the following methods to open the project in PyCharm:

  • Click File → Open in the main menu.

  • Click Open on the welcome page.

After that, find the folder containing the item on your computer and open it.

If the project contains a virtual environment, PyCharm will automatically use the virtual environment and use it as a project interpreter.

If you need to configure a different virtual environment virtualenv, open Preferences on Mac, or use Ctrl+Alt+S to open Settings in Windows or Linux system, and find Project: ProjectName. Open the drop-down list and select Project Interpreter:

Select virtualenv from the drop-down list. If there is no item to be selected, click the setting button on the right of the drop-down list and select Add... The rest of the steps are the same as the steps for creating a new project.

Search and navigate in PyCharm

In large projects, it is difficult for us to remember the location of everything, so fast navigation and search are very important. PyCharm can provide these functions. Next, we use the project opened in the previous section to practice the following shortcut keys:

  • Search for code snippets in the current file: use the Cmd+F key in Mac system, and use Ctrl+F key in Windows or Linux system.

  • Search for code snippets in the entire project: use Cmd+Shift+F keys in Mac systems, and Ctrl+Shift+F keys in Windows or Linux systems.

  • Search category: use Cmd+O key in Mac system, use Ctrl+N key in Windows or Linux system.

  • Search for files: use Cmd+Shift+O in Mac system, and Ctrl+Shift+N in Windows or Linux system.

  • If you don't know whether you want to search for files, classes or code segments, search all: press the Shift key twice.

The following shortcut keys are available for navigation:

  • Go to the declaration of variables: use the Cmd key on Mac systems, the Ctrl key on Windows or Linux systems, and then click on the variable.

  • Find the usage of a class, method or file: use Alt+F7.

  • View recent changes: Use the Shift+Alt+C keys, or click View → Recent Changes in the main menu.

  • View recent files: use the Cmd+E key in Mac system, use Ctrl+E key in Windows or Linux system, or click View → Recent Files in the main menu.

  • Go forward and backward in the navigation history after multiple jumps: use the Cmd+[ / Cmd+] keys in Mac systems, and Ctrl+Alt+Left / Ctrl+Alt+Right keys in Windows or Linux systems.

For more details, see the official document: https://www.jetbrains.com/help/pycharm/tutorial-exploring-navigation-and-search.html.

Version control in PyCharm

Version control systems (such as Git and Mercurial) are one of the most important tools in the modern software development world. Therefore, the IDE must support version control. PyCharm does a great job in this regard, it integrates a large number of popular version control systems, such as Git (and Github (https://github.com/)), Mercurial, Perforce, and Subversion.

Note: The version control system used in the following examples is Git.

Configure Version Control System (VCS)

To achieve VCS integration, you need to click VCS → VCS Operations Popup… in the top menu, or use the Ctrl+V keys in Mac systems, and the Alt+` keys in Windows or Linux systems. Select Enable Version Control Integration..., you will see the following window:

Select Git from the drop-down list, click OK, and you have set up VCS for the project. (Note that if you open an existing project that already has a version control system, PyCharm will find and automatically use the version control system.)

At this time, if you go to VCS Operations Popup..., you will find a different popup window with options git add, git stash, git branch, git commit, git push, etc.:

If you can't find the option you need, you can click on VCS in the top menu and select Git, where you can create and view a pull request.

Submission and conflict handling

These are the two major features of VCS integration in PyCharm, which I personally use often and like very much. If you have completed the work and plan to submit it, go to VCS → VCS Operations Popup… → Commit…, or use the Cmd+K key in Mac system and Ctrl+K key in Windows or Linux system. You will see the following window:

In this window, you can:

  • Select the file to be submitted

  • Write down submission information

  • Perform various checks before submission

  • View changes

  • Click the arrow next to the Commit button in the lower right corner and select Commit and Push... to complete the submission and push at one time.

Does it feel amazing and fast? Especially if you used to perform these tasks manually through the command line.

There will be merge conflicts in teamwork. When one person submits changes to the file you are working on, the two of you change the same line and the changes overlap. At this time, VCS cannot decide whether to choose your change or the teammate's change. Then you can use the following arrows and symbols to solve this problem:

It seems strange that it is difficult for us to tell which changes should be deleted and which changes should be kept. Don't be afraid, PyCharm is here! It can resolve conflicts in a better and more concise way. Go to VCS in the top menu, select Git, and then select Resolve conflicts... Select the conflicting file, click Merge, and the following window will appear:

In the left column, you can view the changes you made. In the right column, you can view the changes made by your teammates. The middle column shows the results. The conflicting code lines are highlighted, and you can see X and >>/<< next to them. Click the arrow to accept the change, and click X to reject the change. After resolving all conflicts, click the Apply button:

In the above figure, for the first conflict line, the author chose to reject his own changes and accept the changes made by his teammates. In the second conflict line, the author accepted his own changes and rejected his teammates' changes.

Many operations can also be performed using the VCS integration in PyCharm. See https://www.jetbrains.com/help/pycharm/version-control-integration.html for details.

Use plugins and external tools in PyCharm

In PyCharm you can find almost all the functions needed for development. If you don't find it, then there is probably a plug-in that provides PyCharm with the functionality you need. For example, they can:

  • Add multi-language and multi-frame support

  • Use shortcut hints, file watchers, etc. to improve your productivity

  • Use code exercises to help you learn new programming languages

For example, the IdeaVim plugin adds Vim simulation to PyCharm. If you like Vim, this plugin can achieve a good combination.

The Material Theme UI plug-in can change the appearance of PyCharm to the appearance of Material Design:

The Vue.js plugin enables PyCharm to support Vue.js projects. The Markdown plug-in makes it possible to edit Markdown files in the IDE and preview the rendered HTML in real time.

Guess you like

Origin blog.csdn.net/qq_38082146/article/details/109364982