How To Write Python Code In Pycharm?
Every code or file within Pycharm is written inside a project. This means, everything in PyCharm is written with respect to a Project and the first thing that you need to create before you can write any code is a project. So, let’s see how we can create a project in PyCharm.
➮ After successfully installing PyCharm, the welcome screen comes up. Click Create New Project.
The Create New Project window opens up.
✶ In this window specify the location of the project where you want to save the files.
✶ Expand the Python Interpreter menu. Here, you can specify whether you want to create a new project interpreter or reuse an existing one. From the drop-down list, you can select one of the options: Virtualenv, Pipenv, or Conda. These are the tools that help us to keep dependencies required by different projects and are separated by creating isolated Python environments for each of them. You can also specify the location of the New environment and select a Base interpreter (for example Python2.x or Python3.x) from the options available. Then we have checkboxes to select Inherit global site-packages and Make available to all projects. Usually, it is a good idea to keep the defaults.
✶ Click Create on the bottom right corner of the dialog box to create the new project.
Note: You might be notified that: Projects can either be opened in a new window or you can replace the project in the existing window or be attached to the already opened projects. How would you like to open the project? Select the desired option.
You might also get a small Tip of the Day popup where PyCharm gives you one trick to learn at each startup. Feel free to close this popup.
Now, we are all set to start writing our first Python code in PyCharm.
- Click on File.
- Choose New.
- Choose Python File and provide a name for the new file. In our case we name it add. Press enter on your keyboard and your new file will be ready and you can write your code in it.
Let us write a simple code that adds two numbers and prints the result of the addition as output.
How To Run The Python Code In PyCharm?
Once the code is written, it is time to run the code. There are three ways of running the Python code in PyCharm.
Method 1: Using Shortcuts
- Use the shortcut Ctrl+Shift+R on Mac to run the code.
- Use the shortcut Ctrl+Shift+F10 on Windows or Linux to run the code.
Method 2: Right click the code window and click on Run ‘add’
Method 3: Choose ‘add’ and click on the little green arrow at the top right corner of the screen as shown in the diagram below.
How To Debug The Code Using Breakpoints?
While coding, you are bound to come across bugs especially if you are working with a tedious production code. PyCharm provides an effective way of debugging your code and allows you to debug your code line by line and identify exceptions or errors with ease. Let us have a look at the following example to visualize how to debug your code in PyCharm.
Note: This is a very basic example and has been just used for the purpose to guide you through the process of debugging in PyCharm. The example computes the average of two numbers but yields different results in the two print statements. A spoiler: we have not used the brackets properly which results in the wrong result in the first case. Now, we will have a look at how we can identify the same by debugging our Python code in PyCharm.
Debugging our code:
Step 1: Setting The Breakpoint
The first requirement to start debugging our code is to place a breakpoint by clicking on the blank space to the left of line number 1 ( this might vary according to your code and requirements). This is the point where the program will be suspended and the process of debugging can be started from here, one line at a time.
Step 2: Start Debugging
Once the breakpoint is set the next step is to start debugging using one of the following ways:
- Using Shortcuts: Ctrl+Shift+D on Mac or Shift+Alt+F9 on Windows or Linux.
- Right-click on the code and choose Debug ‘add’.
- Choose ‘add’ and click on the icon on the top right corner of the menu bar.
Once you use any one of the above methods to start debugging your code, the Debug Window will open up at the bottom as shown in the figure below. Also, note that the current line is highlighted in blue.
Step 3: Debug line by line and identify the error (logical in our case). Press F8 on your keyboard to execute the current line and step over to the next line. To step into the function in the current line, press
F7. As each statement is executed, the changes in the variables are automatically reflected in the Debugger window.
How To Test Code In PyCharm?
For any application or code to be operational, it must undergo unit test and PyCharm facilitates us with numerous testing frameworks for testing our code. The default test runner in Python is
unittest, however, PyCharm also supports other testing frameworks such as
Let us create a file with the name currency.py and then test our file using unit testing.
Now, let us begin unit testing. Follow the steps given below:
Step 1: Create The Test File
To begin testing keep the
currency.py file open and execute any one of the following steps:
- Use Shortcut: Press
Shift+Cmd+Ton Mac or Ctrl+Shift+T on Windows or Linux.
- Right-click on the class and select Go To ➠ Test. Make sure you right-click on the name of the class to avoid confusion!
- Go to the main menu ➠ select Navigate ➠ Select Test
Step 2: Select Create New Test and that opens up the Create Test window. Keep the defaults and select all the methods and click on OK.
✶ PyCharm will automatically create a file with the name
test_currency.py with the following tests within it.
Step 3: Create the Test Cases
Once our test file is created we need to import the currency class within it and define the test cases as follows:
Step 4: Run the Unit Test
Now, we need to run the test using one of the following methods:
- Using Shortcut: Press Ctrl+R on Mac or Shift+F10 on Windows or Linux.
- Right-click and choose Run ‘Unittests for test_currency.py’.
- Click on the green arrow to the left of the test class name and choose Run ‘Unittests for test_currency.py’.
You will since that two tests are successful while one test fails. To be more specific unit test for test_euro() and test_yen() are successful while the test fails for test_pound().
That brings us to the end of this section and it is time for us to move on to a very important section of our tutorial where we will be discussing numerous tips and tricks to navigate PyCharm with the help of some interesting shortcuts. We will also discuss in brief about some of the tools like Django that we can integrate with PyCharm. So, without further delay lets dive into to next section.
Please click on the Next link/button given below to move on to the next phase of PyCharm journey!