Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Writing, Running, Debugging, and Testing Code In PyCharm

#1
Writing, Running, Debugging, and Testing Code In PyCharm

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.

Example:


Output:


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 pytestnosedoctesttox, and trial.

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:

  1. Use Shortcut: Press Shift+Cmd+T on Mac or Ctrl+Shift+T on Windows or Linux.
  2. Right-click on the class and select Go To ➠ Test. Make sure you right-click on the name of the class to avoid confusion!
  3. 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().

Output:


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!






The post Writing, Running, Debugging, and Testing Code In PyCharm first appeared on Finxter.



https://www.sickgaming.net/blog/2020/11/...n-pycharm/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Writing Clean Code — Being a Professional [Cheat Sheet + Video] xSicKxBot 0 41 03-16-2021, 01:04 PM
Last Post: xSicKxBot
  [Tut] Best VS Code Cheat Sheet xSicKxBot 0 63 02-25-2021, 05:33 PM
Last Post: xSicKxBot
  [Tut] 14 Unix Principles to Write Better Code xSicKxBot 0 92 12-29-2020, 05:21 AM
Last Post: xSicKxBot
  [Tut] 7 Tips to Write Clean Code xSicKxBot 0 95 11-26-2020, 03:17 AM
Last Post: xSicKxBot
  [Tut] How to Fix “ImportError: No module named pandas” [Mac/Linux/Windows/PyCharm] xSicKxBot 0 125 11-08-2020, 04:36 AM
Last Post: xSicKxBot
  [Tut] Lost Your Sight? How to Code as a Blind or Visually-Impaired Python Developer xSicKxBot 0 177 08-11-2020, 02:49 PM
Last Post: xSicKxBot
  [Tut] Why You Need to Stop Learning to Code [Video] xSicKxBot 0 220 05-08-2020, 04:42 AM
Last Post: xSicKxBot
  [Tut] Print a Python List Beautifully [Click & Run Code] xSicKxBot 0 250 04-23-2020, 02:39 AM
Last Post: xSicKxBot
  [Tut] A Simple Python Morse Code Translator xSicKxBot 0 331 02-22-2020, 11:47 AM
Last Post: xSicKxBot

Forum Jump:

[-]
Active Threads
[Tut] Iterating Through a Range of Dates...
Last Post: xSicKxBot
Yesterday 09:18 PM
» Replies: 0
» Views: 4
(Indie Deal) FREE Super Panda Adventures...
Last Post: xSicKxBot
Yesterday 09:18 PM
» Replies: 0
» Views: 4
Mobile - RuneScape Mobile – what Jagex l...
Last Post: xSicKxBot
Yesterday 09:18 PM
» Replies: 0
» Views: 4
ASP.NET Core 6 and Authentication Server...
Last Post: xSicKxBot
Yesterday 09:18 PM
» Replies: 0
» Views: 4
AppleInsider - Mj Rodriguez joins Apple ...
Last Post: xSicKxBot
Yesterday 09:17 PM
» Replies: 0
» Views: 5
News - Random: This Minecraft Version Of...
Last Post: xSicKxBot
Yesterday 09:17 PM
» Replies: 0
» Views: 4
News - Don't Miss Vinny, Alex, And Brad'...
Last Post: xSicKxBot
Yesterday 09:17 PM
» Replies: 0
» Views: 39
Xbox Wire - Xbox and NTWRK Collaborate t...
Last Post: xSicKxBot
Yesterday 02:05 PM
» Replies: 0
» Views: 5
News - Nintendo Game Builder Garage Game...
Last Post: xSicKxBot
Yesterday 02:05 PM
» Replies: 0
» Views: 54
News - Get a job: Join Talofa Games as a...
Last Post: xSicKxBot
Yesterday 02:05 PM
» Replies: 0
» Views: 54

[-]
Twitter

Copyright © SickGaming.net 2012-2020