Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] [FIXED] Carriage return Not Working with Print in VS Code

#1
[FIXED] Carriage return Not Working with Print in VS Code

<div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{&quot;align&quot;:&quot;left&quot;,&quot;id&quot;:&quot;405628&quot;,&quot;slug&quot;:&quot;default&quot;,&quot;valign&quot;:&quot;top&quot;,&quot;reference&quot;:&quot;auto&quot;,&quot;count&quot;:&quot;0&quot;,&quot;readonly&quot;:&quot;&quot;,&quot;score&quot;:&quot;0&quot;,&quot;best&quot;:&quot;5&quot;,&quot;gap&quot;:&quot;5&quot;,&quot;greet&quot;:&quot;Rate this post&quot;,&quot;legend&quot;:&quot;0\/5 - (0 votes)&quot;,&quot;size&quot;:&quot;24&quot;,&quot;width&quot;:&quot;0&quot;,&quot;_legend&quot;:&quot;{score}\/{best} - ({count} {votes})&quot;}">
<div class="kksr-stars">
<div class="kksr-stars-inactive">
<div class="kksr-star" data-star="1" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="2" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="3" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="4" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" data-star="5" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
<div class="kksr-stars-active" style="width: 0px;">
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
<div class="kksr-star" style="padding-right: 5px">
<div class="kksr-icon" style="width: 24px; height: 24px;"></div>
</p></div>
</p></div>
</div>
<div class="kksr-legend"> <span class="kksr-muted">Rate this post</span> </div>
</div>
<p class="has-global-color-8-background-color has-background"><strong>FIX: </strong>To fix the carriage return now working issue in your IDE, you must directly use the <strong>terminal</strong> to execute the code instead of using the built-in output console provided by the IDE.</p>
<h2><strong>Problem Formulation</strong></h2>
<p>It is a common issue in many IDEs, including VS Code and PyCharm, wherein the carriage return character (<strong>‘\r’</strong>) does not work properly within the <code>print</code> statement. </p>
<p><strong>Example: </strong>Consider the following code where we are attempting to overwrite the previous print to the same line:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time
li = ['start', 'Processing result']
for i in range(len(li)): print(li[i], end='\r') time.sleep(2)
print(end='\x1b[2K') # ANSI sequence to clear the line where the cursor is located
print('Terminate')</pre>
<p><strong>Expected Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="410" height="178" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-46.png" alt="" class="wp-image-405635" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-46.png 410w, https://blog.finxter.com/wp-content/uplo...00x130.png 300w" sizes="(max-width: 410px) 100vw, 410px" /></figure>
<p><strong>Actual Output: </strong>Unfortunately, when we execute this code in <strong>VS Code</strong> and run it in the OUTPUT console, this is how the output looks:</p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="600" height="287" src="https://blog.finxter.com/wp-content/uploads/2022/06/carriage_return_problem.gif" alt="" class="wp-image-406628" /></figure>
<p><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f6d1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> Thus, the actual output defeats the purpose of the code as the print statement displays the strings in new lines, which is exactly what we want to avoid. </p>
<p><strong>Reason: </strong>The question here is – “<em>Is the code wrong?</em>” Well, there is no issue with our code. Let’s get to the root of the problem. </p>
<p class="has-background" style="background-color:#ecb9f7">The OUTPUT console in VS Code exhibits a slightly different behavior than the standard output terminal. Some GUI-based IDEs don’t work properly for the Carriage return character (“<strong>\r</strong>“).  Hence, even though the code is correct, the output console of the IDE is not working properly for the carriage return within the print statement.</p>
<p class="has-base-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4cc.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /><strong>Highly Recommended Read: <a href="https://blog.finxter.com/how-to-overwrite-the-previous-print-to-stdout-in-python/" target="_blank" rel="noreferrer noopener">How to Overwrite the Previous Print to Stdout in Python?</a></strong></p>
<h2><strong>Solution</strong></h2>
<p>The straightforward solution to this problem is to run the code in <strong>the standard output terminal</strong> instead of executing the code in the output console. </p>
<p><strong>Note:</strong> If you are facing problems with buffering the output you can use the <code>flush='True'</code><strong> </strong>parameter within the print statement as shown below.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="4" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import time
li = ['start', 'Processing result']
for i in range(len(li)): print(li[i], end='\r', flush=True) time.sleep(2)
print('Terminate')</pre>
<p>Let’s dive into the different ways to execute the code in the terminal to get the desired output:</p>
<h3><strong>Method 1</strong></h3>
<ul>
<li> Select <strong>Terminal</strong></li>
</ul>
<figure class="wp-block-image size-large is-style-default"><img loading="lazy" width="1024" height="384" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-58-1024x384.png" alt="" class="wp-image-406661" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-58-1024x384.png 1024w, https://blog.finxter.com/wp-content/uplo...00x112.png 300w, https://blog.finxter.com/wp-content/uplo...68x288.png 768w, https://blog.finxter.com/wp-content/uplo...age-58.png 1046w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<ul>
<li>Select the PATH, which contains the .py script. In my case, it is: <code>D:\VS Code Python Scripts</code>. So this is the command to navigate to this path (in WINDOWS): <em><code>cd 'D:\VS Code Python Scripts'</code></em>
<ul>
<li>Note that I have used <code>' '</code> to enclose the path to avoid any command-line error because of the spacing in the filename.</li>
</ul>
</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="514" height="128" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-59.png" alt="" class="wp-image-406662" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-59.png 514w, https://blog.finxter.com/wp-content/uplo...300x75.png 300w" sizes="(max-width: 514px) 100vw, 514px" /></figure>
<ul>
<li>Once you are at the specified Path, use the normal Python command to run your script: <code>python same_line_print.py</code></li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="500" height="116" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-60.png" alt="" class="wp-image-406664" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-60.png 500w, https://blog.finxter.com/wp-content/uplo...300x70.png 300w" sizes="(max-width: 500px) 100vw, 500px" /></figure>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="368" height="87" src="https://blog.finxter.com/wp-content/uploads/2022/06/output.gif" alt="" class="wp-image-406672" /></figure>
<h3><strong>Method 2</strong></h3>
<ul>
<li>Right-click on the code</li>
<li>Select <strong>Run Python file Terminal</strong></li>
</ul>
<figure class="wp-block-image size-large is-style-default"><img loading="lazy" width="1024" height="576" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-61-1024x576.png" alt="" class="wp-image-406675" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-61-1024x576.png 1024w, https://blog.finxter.com/wp-content/uplo...00x169.png 300w, https://blog.finxter.com/wp-content/uplo...68x432.png 768w, https://blog.finxter.com/wp-content/uplo...age-61.png 1366w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<h3><strong>Method 3</strong></h3>
<p>If you are using the <strong>Code Runner Extension</strong> to run your code in VS Code:</p>
<ul>
<li>Click on the  down arrow button just beside the Run button. A drop down menu appears.</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="279" height="121" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-62.png" alt="" class="wp-image-406682" /></figure>
<ul>
<li>Select Run Python File (Don’t select Run Code)</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="243" height="159" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-63.png" alt="" class="wp-image-406683" /></figure>
<h2>Solving the Issue in PyCharm</h2>
<p>The same issue can be observed in the PyCharm IDE as well. The solution in this case is quite similar, i.e., run the code directly in the terminal. </p>
<ul>
<li>Select Terminal</li>
</ul>
<figure class="wp-block-image size-large is-style-default"><img loading="lazy" width="1024" height="153" src="https://blog.finxter.com/wp-content/uploads/2022/06/image-64-1024x153.png" alt="" class="wp-image-407949" srcset="https://blog.finxter.com/wp-content/uploads/2022/06/image-64-1024x153.png 1024w, https://blog.finxter.com/wp-content/uplo...300x45.png 300w, https://blog.finxter.com/wp-content/uplo...68x115.png 768w, https://blog.finxter.com/wp-content/uplo...age-64.png 1135w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>
<ul>
<li>Type the normal Python command to execute the program:
<ul>
<li><code>python 'carriage return.py'</code></li>
</ul>
</li>
</ul>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="600" height="337" src="https://blog.finxter.com/wp-content/uploads/2022/06/carriage_return_PyCharm.gif" alt="" class="wp-image-407945" /></figure>
<h2><strong>Conclusion</strong></h2>
<p>Thus, the bottom line is – Though the code is correct, it is the console of the IDE that misbehaves and obstructs the carriage return, which denies us the kind of output we want. Hence, the simple solution to this is to use the terminal to run your code from within the IDE. </p>
<p class="has-base-background-color has-background"><strong>Related Read:</strong> <strong><a rel="noreferrer noopener" href="https://blog.finxter.com/best-python-ide/" target="_blank">Best Python IDE and Code Editors [Ultimate Guide]</a></strong></p>
<p>I hope this tutorial helped you. Please <strong><a href="https://blog.finxter.com/subscribe/">subscribe</a></strong> and <strong><a href="https://www.youtube.com/c/FinxterCreateYourCodingBusiness" target="_blank" rel="noreferrer noopener">stay tuned</a></strong> for more solutions and tutorials. Happy learning! <img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f642.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></p>
<hr class="wp-block-separator has-alpha-channel-opacity" />
<p>To become a PyCharm master, check out our <a href="https://academy.finxter.com/university/pycharm/" title="https://academy.finxter.com/university/pycharm/" target="_blank" rel="noreferrer noopener">full course</a> on the Finxter Computer Science Academy available for free for all <a href="https://blog.finxter.com/finxter-premium-membership/" target="_blank" rel="noreferrer noopener" title="Finxter Premium Membership">Finxter Premium Members</a>:</p>
<div class="wp-block-image">
<figure class="aligncenter size-full"><a href="https://academy.finxter.com/university/pycharm/" target="_blank" rel="noopener"><img loading="lazy" width="363" height="650" src="https://blog.finxter.com/wp-content/uploads/2021/09/image-10.png" alt="" class="wp-image-34968" srcset="https://blog.finxter.com/wp-content/uploads/2021/09/image-10.png 363w, https://blog.finxter.com/wp-content/uplo...68x300.png 168w" sizes="(max-width: 363px) 100vw, 363px" /></a></figure>
</div>
</div>


https://www.sickgaming.net/blog/2022/06/...n-vs-code/
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016