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

<div><h2>How To <span style="color:#ff6900" class="has-inline-color">Write </span>Python Code In Pycharm?</h2>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="4. Writing and Running Code In PyCharm" width="1400" height="788" src="https://www.youtube.com/embed/FCbuXmGqYUY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>Every code or file within Pycharm is written inside a <strong>project</strong>. 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 <strong>project.</strong> So, let’s see how we can create a project in PyCharm.</p>
<p>➮ After successfully installing PyCharm, the welcome screen comes up. Click <strong>Create New Project</strong>. </p>
<figure class="wp-block-image size-large is-resized"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-247.png" alt="" class="wp-image-16213" width="487" height="352" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-247.png 658w, 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" /></figure>
<p>The Create New Project window opens up.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="795" height="455" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-212.png" alt="" class="wp-image-15982" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-212.png 795w, 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" /></figure>
<p>✶ In this window specify the location of the project where you want to save the files.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="790" height="44" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-209.png" alt="" class="wp-image-15963" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-209.png 790w, 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" /></figure>
<p>✶ 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&nbsp;the options: <strong>Virtualenv,&nbsp;Pipenv, or&nbsp;Conda</strong>. 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<strong> Inherit global site-packages</strong> and <strong>Make available to all projects</strong>. Usually, it is a good idea to keep the defaults. </p>
<figure class="wp-block-image size-large"><img loading="lazy" width="786" height="231" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-210.png" alt="" class="wp-image-15972" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-210.png 786w, 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" /></figure>
<p>✶ Click <strong>Create</strong> on the bottom right corner of the dialog box to create the new project.</p>
<p><strong><span style="text-decoration: underline">Note:</span></strong> 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.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="800" height="107" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-211.png" alt="" class="wp-image-15981" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-211.png 800w, 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" /></figure>
<p>You might also get a small&nbsp;<em>Tip of the Day</em>&nbsp;popup where PyCharm gives you one trick to learn at each startup. Feel free to close this popup.</p>
<p>Now, we are all set to start writing our first Python code in PyCharm. </p>
<ul>
<li>Click on<strong> File.</strong></li>
<li>Choose <strong>New</strong>.</li>
</ul>
<figure class="wp-block-image size-large is-resized"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-213.png" alt="" class="wp-image-15983" width="269" height="368" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-213.png 354w, 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" /></figure>
<ul>
<li>Choose <strong>Python File</strong> and provide a name for the new file. In our case we name it <strong>add</strong>. Press enter on your keyboard and your new file will be ready and you can write your code in it.</li>
</ul>
<figure class="wp-block-image size-large"><img loading="lazy" width="343" height="145" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-214.png" alt="" class="wp-image-15984" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-214.png 343w, 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" /></figure>
<p>Let us write a simple code that adds two numbers and prints the result of the addition as output. </p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="207" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-215-1024x207.png" alt="" class="wp-image-15993" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-215-1024x207.png 1024w, 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" /></figure>
<h2>How To <span style="color:#ff6900" class="has-inline-color">Run </span>The Python Code In PyCharm?</h2>
<p>Once the code is written, it is time to run the code. There are <strong>three </strong>ways of running the Python code in PyCharm.</p>
<p><strong>Method 1: Using Shortcuts</strong></p>
<ul>
<li>Use the shortcut&nbsp;<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>R</kbd>&nbsp;on Mac to run the code.</li>
<li>Use the shortcut&nbsp;<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>F10</kbd>&nbsp;on Windows or Linux to run the code.</li>
</ul>
<p><strong>Method 2: Right click the code window and click</strong> <strong>on <span style="text-decoration: underline">Run ‘add’</span></strong></p>
<figure class="wp-block-image size-large"><img loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2020/10/run-code.gif" alt="" class="wp-image-15995" /></figure>
<p><strong>Method 3: </strong>Choose&nbsp;<em>‘add’</em>&nbsp; and click on the little green arrow at the top right corner of the screen as shown in the diagram below.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="1024" height="54" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-216-1024x54.png" alt="" class="wp-image-15994" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-216-1024x54.png 1024w, 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" /></figure>
<h2>How To <span style="color:#ff6900" class="has-inline-color">Debug </span>The Code Using Breakpoints?</h2>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="5. Debugging In PyCharm" width="1400" height="788" src="https://www.youtube.com/embed/Oz8zmjcO8cw?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>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.</p>
<p><strong>Example:</strong></p>
<figure class="wp-block-image size-large"><img loading="lazy" width="502" height="273" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-218.png" alt="" class="wp-image-15998" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-218.png 502w, 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" /></figure>
<p> <strong>Output: </strong></p>
<figure class="wp-block-image size-large"><img loading="lazy" width="653" height="142" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-219.png" alt="" class="wp-image-15999" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-219.png 653w, 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" /></figure>
<p><strong>Note: </strong>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. <span style="text-decoration: underline">A spoiler:</span> 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. </p>
<p><strong>Debugging our code:</strong></p>
<p><strong>Step 1: Setting The Breakpoint</strong></p>
<p>The first requirement to start debugging our code is to place a <strong>breakpoint </strong>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.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="681" height="383" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-220.png" alt="" class="wp-image-16000" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-220.png 681w, 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" /></figure>
<p><strong>Step 2: Start Debugging</strong></p>
<p>Once the breakpoint is set the next step is to start debugging using one of the following ways:</p>
<ul>
<li><strong>Using Shortcuts:</strong>&nbsp;<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>D</kbd>&nbsp;on Mac or&nbsp;<kbd>Shift</kbd>+<kbd>Alt</kbd>+<kbd>F9</kbd>&nbsp;on Windows or Linux.</li>
<li>Right-click on the code and choose <strong>Debug ‘add’</strong>.</li>
<li>Choose&nbsp;<em>‘add’</em>&nbsp; and click on the <img loading="lazy" width="27" height="28" class="wp-image-16001" style="width: 27px" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-221.png" alt=""> icon on the top right corner of the menu bar.</li>
</ul>
<p>Once you use any one of the above methods to start debugging your code, the <strong>Debug Window</strong> will open up at the bottom as shown in the figure below. Also, note that the current line is highlighted in blue.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="747" height="359" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-223.png" alt="" class="wp-image-16004" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-223.png 747w, 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" /></figure>
<p><strong>Step 3:</strong> Debug line by line and identify the error (logical in our case). Press&nbsp;<kbd>F8</kbd>&nbsp;on your keyboard to execute the current line and<strong> step&nbsp;over</strong>&nbsp;to the next line. To <strong>step&nbsp;into</strong>&nbsp;the function in the current line, press <code>F7</code>. As each statement is executed, the changes in the variables are automatically reflected in the Debugger window. </p>
<figure class="wp-block-image size-large"><img loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2020/10/debug.gif" alt="" class="wp-image-16005" /></figure>
<h2>How To <span style="color:#ff6900" class="has-inline-color">Test </span>Code In PyCharm?</h2>
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
<div class="wp-block-embed__wrapper">
<div class="ast-oembed-container"><iframe title="6. Testing In PyCharm" width="1400" height="788" src="https://www.youtube.com/embed/PJ6uXtNdjjo?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>
</div>
</figure>
<p>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 <code>unittest</code>, however, PyCharm also supports other testing frameworks such as&nbsp;<code>pytest</code>,&nbsp;<code>nose</code>,&nbsp;<code>doctest</code>,&nbsp;<code>tox</code>, and&nbsp;<code>trial</code>. </p>
<p>Let us create a file with the name <strong>currency.py</strong> and then test our file using unit testing.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="435" height="221" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-224.png" alt="" class="wp-image-16007" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-224.png 435w, 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" /></figure>
<p> Now, let us begin unit testing. Follow the steps given below:</p>
<p><strong>Step 1:</strong> <strong>Create The Test File</strong></p>
<p>To begin testing keep the <code>currency.py</code> file open and execute any one of the following steps:</p>
<ol>
<li><strong>Use Shortcut:</strong> Press&nbsp;<code><kbd>Shift</kbd>+<kbd>Cmd</kbd>+<kbd>T</kbd></code>&nbsp;on Mac or&nbsp;<kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>T</kbd>&nbsp;on Windows or Linux.</li>
<li>Right-click on the class and select&nbsp;<em>Go To</em>&nbsp;➠&nbsp;<em>Test</em>. Make sure you right-click on the name of the class to avoid confusion!</li>
<li>Go to the main menu ➠ select Navigate ➠ Select Test</li>
</ol>
<p><strong>Step 2: </strong>Select <strong>Create New Test</strong> and that opens up the Create Test window. Keep the defaults and select all the methods and click on <strong>OK</strong>. </p>
<figure class="wp-block-image size-large"><img loading="lazy" width="524" height="488" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-225.png" alt="" class="wp-image-16008" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-225.png 524w, 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" /></figure>
<p>✶ PyCharm will automatically create a file with the name <code>test_currency.py</code> with the following tests within it.</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="364" height="276" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-226.png" alt="" class="wp-image-16009" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-226.png 364w, 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" /></figure>
<p><strong>Step 3: Create the Test Cases</strong></p>
<p>Once our test file is created we need to import the currency class within it and define the test cases as follows:</p>
<figure class="wp-block-image size-large"><img loading="lazy" width="567" height="403" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-1.png" alt="" class="wp-image-16216" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-1.png 567w, 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" /></figure>
<p><strong> Step 4:</strong> <strong>Run the Unit Test</strong></p>
<p>Now, we need to run the test using one of the following methods:</p>
<ul>
<li><strong>Using Shortcut: </strong>Press&nbsp;<kbd>Ctrl</kbd>+<kbd>R</kbd>&nbsp;on Mac or&nbsp;<kbd>Shift</kbd>+<kbd>F10</kbd>&nbsp;on Windows or Linux. </li>
<li>Right-click and choose&nbsp;<em>Run ‘Unittests for test_currency.py’</em>.</li>
<li>Click on the green arrow to the left of the test class name and choose&nbsp;<em>Run ‘Unittests for test_currency.py’</em>.</li>
</ul>
<p>You will since that two tests are successful while one test fails. To be more specific unit test for <strong>test_euro() </strong>and <strong>test_yen() </strong>are successful while the test fails for <strong>test_pound()</strong>.</p>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-large"><img loading="lazy" width="697" height="257" src="https://blog.finxter.com/wp-content/uploads/2020/11/image-2.png" alt="" class="wp-image-16217" srcset="https://blog.finxter.com/wp-content/uploads/2020/11/image-2.png 697w, 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" /></figure>
<p>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. </p>
<p>Please click on the <strong>Next</strong> link/button given below to move on to the next phase of PyCharm journey!</p>
<p> <a href="https://blog.finxter.com/pycharm-a-simple-illustrated-guide/" target="_blank" rel="noopener noreferrer"><br />
<button type="button" style="background-color: #4CAF50;border: none">Previous</button><br />
</a><br />
<a href="#" rel="noopener noreferrer"><br />
<button type="button" style="background-color: #4CAF50;border: none">Next</button><br />
</a></p>
<p>The post <a href="https://blog.finxter.com/writing-running-debugging-and-testing-code-in-pycharm/" target="_blank" rel="noopener noreferrer">Writing, Running, Debugging, and Testing Code In PyCharm</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p>
</div>


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



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016