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.

https://blog.finxter.com/wp-content/uplo...00x217.png 300w, https://blog.finxter.com/wp-content/uplo...50x109.png 150w" sizes="(max-width: 487px) 100vw, 487px" />

The Create New Project window opens up.

https://blog.finxter.com/wp-content/uplo...00x172.png 300w, https://blog.finxter.com/wp-content/uplo...68x440.png 768w, https://blog.finxter.com/wp-content/uplo...150x86.png 150w" sizes="(max-width: 795px) 100vw, 795px" />

✶ In this window specify the location of the project where you want to save the files.

https://blog.finxter.com/wp-content/uplo...300x17.png 300w, https://blog.finxter.com/wp-content/uplo...768x43.png 768w, https://blog.finxter.com/wp-content/uplo...-150x8.png 150w" sizes="(max-width: 790px) 100vw, 790px" />

✶ 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.

https://blog.finxter.com/wp-content/uplo...300x88.png 300w, https://blog.finxter.com/wp-content/uplo...68x226.png 768w, https://blog.finxter.com/wp-content/uplo...150x44.png 150w" sizes="(max-width: 786px) 100vw, 786px" />

✶ 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.

https://blog.finxter.com/wp-content/uplo...300x40.png 300w, https://blog.finxter.com/wp-content/uplo...68x103.png 768w, https://blog.finxter.com/wp-content/uplo...150x20.png 150w" sizes="(max-width: 800px) 100vw, 800px" />

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.
https://blog.finxter.com/wp-content/uplo...19x300.png 219w, https://blog.finxter.com/wp-content/uplo...50x205.png 150w" sizes="(max-width: 269px) 100vw, 269px" />

  • 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.
https://blog.finxter.com/wp-content/uplo...00x127.png 300w, https://blog.finxter.com/wp-content/uplo...150x63.png 150w" sizes="(max-width: 343px) 100vw, 343px" />

Let us write a simple code that adds two numbers and prints the result of the addition as output.

https://blog.finxter.com/wp-content/uplo...300x61.png 300w, https://blog.finxter.com/wp-content/uplo...68x155.png 768w, https://blog.finxter.com/wp-content/uplo...150x30.png 150w, https://blog.finxter.com/wp-content/uplo...ge-215.png 1358w" sizes="(max-width: 1024px) 100vw, 1024px" />

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.

https://blog.finxter.com/wp-content/uplo...300x16.png 300w, https://blog.finxter.com/wp-content/uplo...768x40.png 768w, https://blog.finxter.com/wp-content/uplo...-150x8.png 150w, https://blog.finxter.com/wp-content/uplo...ge-216.png 1357w" sizes="(max-width: 1024px) 100vw, 1024px" />

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:

https://blog.finxter.com/wp-content/uplo...00x163.png 300w, https://blog.finxter.com/wp-content/uplo...150x82.png 150w" sizes="(max-width: 502px) 100vw, 502px" />

Output:

https://blog.finxter.com/wp-content/uplo...300x65.png 300w, https://blog.finxter.com/wp-content/uplo...150x33.png 150w" sizes="(max-width: 653px) 100vw, 653px" />

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.

https://blog.finxter.com/wp-content/uplo...00x169.png 300w, https://blog.finxter.com/wp-content/uplo...150x84.png 150w" sizes="(max-width: 681px) 100vw, 681px" />

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.

https://blog.finxter.com/wp-content/uplo...00x144.png 300w, https://blog.finxter.com/wp-content/uplo...150x72.png 150w" sizes="(max-width: 747px) 100vw, 747px" />

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.

https://blog.finxter.com/wp-content/uplo...00x152.png 300w, https://blog.finxter.com/wp-content/uplo...150x76.png 150w" sizes="(max-width: 435px) 100vw, 435px" />

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.

https://blog.finxter.com/wp-content/uplo...00x279.png 300w, https://blog.finxter.com/wp-content/uplo...50x140.png 150w" sizes="(max-width: 524px) 100vw, 524px" />

✶ PyCharm will automatically create a file with the name test_currency.py with the following tests within it.

https://blog.finxter.com/wp-content/uplo...00x227.png 300w, https://blog.finxter.com/wp-content/uplo...50x114.png 150w" sizes="(max-width: 364px) 100vw, 364px" />

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:

https://blog.finxter.com/wp-content/uplo...00x213.png 300w, https://blog.finxter.com/wp-content/uplo...50x107.png 150w" sizes="(max-width: 567px) 100vw, 567px" />

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:

https://blog.finxter.com/wp-content/uplo...00x111.png 300w, https://blog.finxter.com/wp-content/uplo...150x55.png 150w" sizes="(max-width: 697px) 100vw, 697px" />

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] How to Fix “ImportError: No module named pandas” [Mac/Linux/Windows/PyCharm] xSicKxBot 0 21 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 94 08-11-2020, 02:49 PM
Last Post: xSicKxBot
  [Tut] Why You Need to Stop Learning to Code [Video] xSicKxBot 0 166 05-08-2020, 04:42 AM
Last Post: xSicKxBot
  [Tut] Print a Python List Beautifully [Click & Run Code] xSicKxBot 0 184 04-23-2020, 02:39 AM
Last Post: xSicKxBot
  [Tut] A Simple Python Morse Code Translator xSicKxBot 0 240 02-22-2020, 11:47 AM
Last Post: xSicKxBot

Forum Jump:

Become a Patron!
[-]
Upcoming Events

[-]
Latest Threads
Official Sick Gaming Social Links: Twitt...
Last Post: Richarddat
Today 10:17 AM
» Replies: 1
» Views: 1684
Online Coursework Proofreading Service
Last Post: Berenice5
Today 10:09 AM
» Replies: 0
» Views: 2
Microsoft - Making credit flow again in ...
Last Post: xSicKxBot
Today 08:43 AM
» Replies: 0
» Views: 1
News - Gal*Gun Returns Scores February 2...
Last Post: xSicKxBot
Today 08:42 AM
» Replies: 0
» Views: 1
Nike Flyknit Racer Pistachio
Last Post: xm56h2wh
Today 06:21 AM
» Replies: 0
» Views: 4
Adidas NMD CS2 PK Pearl Grey
Last Post: xm56h2wh
Today 06:19 AM
» Replies: 0
» Views: 3
Adidas NMD CS2 Core Black/Red Solid
Last Post: xm56h2wh
Today 06:18 AM
» Replies: 0
» Views: 3
Unigine 2.13 Released
Last Post: xSicKxBot
Today 02:25 AM
» Replies: 0
» Views: 2
News - Defentron Brings ’80s-Style 3D To...
Last Post: xSicKxBot
Today 02:24 AM
» Replies: 0
» Views: 1
News - Soccer Pros Are Concerned About F...
Last Post: xSicKxBot
Today 02:24 AM
» Replies: 0
» Views: 3

[-]
Twitter

[-]
Sponsored
Get the Deal of the Week at RefurBees.com

Copyright © SickGaming.net 2012-2020