<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML lang="en">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252">
<link href="style.css" rel="stylesheet" type="text/css">
<title>4.6.4. Running Python &amp; wxPython</title>
<script src="show_matches.js" type="text/javascript"></script>
</HEAD>
<BODY>
<h3>4.6.4. Running Python &amp; wxPython</h3>

<p><b>1. Interactive Python inside NppExec</b></p>
<p>Running &quot;python -?&quot; gives the following help information, in particular:</p>
<ul>
  <li>-i : inspect interactively after running script, (also PYTHONINSPECT=x) and force prompts, <em>even if stdin does not appear to be a terminal</em></li>
  <li>-u : <em>unbuffered binary stdout and stderr</em> (also PYTHONUNBUFFERED=x), see man page for details on internal buffering relating to '-u'</li>
</ul>
<p>The text in italic <em>exactly</em> refers to what is stated in NppExec's Manual, sections 1.3 and 3.1 by &quot;NppExec is not a console emulator&quot;.</p>
<p>So, by running &quot;python -i -u&quot; in NppExec's Console, you get the interactive Python inside Notepad++.</p>
<p>Here is an advanced example of NppExec's script to be used to test a Python's program interactively:</p>
<blockquote>
  <pre>npp_console local -  // disable any output to the Console
npp_save  // save current file (a .py file is expected)
cd &quot;$(CURRENT_DIRECTORY)&quot;  // use the current file's dir
set local @exit_cmd_silent = exit()  // allows to exit Python automatically
env_set local PATH = $(SYS.PATH);C:\Python27  // using Python 2.7
npp_setfocus con  // set the focus to the Console
npp_console local +  // enable output to the Console
python -i -u &quot;$(FILE_NAME)&quot;  // run Python's program interactively
</pre>
</blockquote>
<blockquote>
  <pre> </pre>
</blockquote>

<p><b>2. Python and UTF-8</b></p>
<p>If your python's program contains some non-ASCII characters, you can get the following error from Python:</p>
<blockquote>
  <pre>SyntaxError: Non-ASCII character</pre>
</blockquote>
<p>To be able to represent such non-ASCII characters correctly on any system, 
such source file should be saved as UTF-8 (either without BOM or with BOM). 
Though the error mentioned above still remains. 
To avoid this error, acccording to <code>https://peps.python.org/pep-0263/</code> , 
you just need to specify</p>
<blockquote>
  <pre># coding=utf-8</pre>
</blockquote>
<p>or</p>
<blockquote>
  <pre># -*- coding: utf-8 -*-</pre>
</blockquote>
<p>at the beginning of your python's program.</p>
<p>Another thing is to output something to console as UTF-8. In this case, you can get something similar from Python:</p>
<blockquote>
  <pre>UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to &lt;undefined&gt;</pre>
</blockquote>
<p>To fix this last one, it's enough to specify the environment variable PYTHONIOENCODING by setting it to &quot;utf-8&quot;. I.e.</p>
<blockquote>
  <pre>// within NppExec
env_set local PYTHONIOENCODING=utf-8  // UTF-8 output for Python
npe_console local -- o2 i2            // UTF-8 output/input for NppExec's Console
python my_program.py</pre>
</blockquote>
<blockquote>
  <pre> </pre>
</blockquote>

<p><b>3. Running Python scripts using wxPython</b></p>
<p><em>[the text below has been originally posted by cioma in NppExec's forum]</em></p>
<p>[ cioma: ]</p>
<blockquote>
  <p>I use NPP as an IDE for Python. And I use NppExec to run scripts directly from NPP, highlight script syntax and runtime errors (if any) and link errors to a line of code. </p>
  <p>Some time ago I started using wxPython GUI library and faced problems with running such scripts from within NppExec. </p>
  <p>If I run this (in NppExec prompt): </p>
  <blockquote>
    <pre>python -t -B -u &quot;$(FULL_CURRENT_PATH)&quot; </pre>
  </blockquote>
  <p>...then GUI part of wxPython is not shown. I guess the reason is that wxPython requires a &quot;real&quot; console buffer and NppExec doesn't provide that. </p>
  <p>If I just run script over NPP &quot;Run&quot; dialog (no NppExec) then GUI is shown but if there are errors there is no way to easily relate them to line of code in NPP. </p>
  <p>So I found this solution to work: </p>
  <p>1. When creating wxPython application in the script make sure it's STDOUT is not redirected: </p>
  <blockquote>
    <pre>app = App(redirect=False) </pre>
  </blockquote>
  <p>2. Run in NppExec: </p>
  <blockquote>
    <pre>cmd /C python -t -B -u &quot;$(FULL_CURRENT_PATH)&quot; </pre>
  </blockquote>
  <p>Voila! We have both wxPython GUI running and its STDOUT redirected to NppExec. </p>
</blockquote>
<p>[ DV: ]</p>
<blockquote>
  <p>Just one thing. For more details regarding runtime errors parsing (keyword: Highlight filters), refer to <a href="4.7.4.html">[4.7.4]</a> and &quot;help con_filter&quot;. </p>
</blockquote>
<blockquote>
  <pre> </pre>
</blockquote>

<p><b>4. Running Pygame Code</b></p>
<p><em>[the text below has been originally posted by Ahmed A. in Notepad++'s forum]</em></p>
<p>I have NppExec set up with this script:</p>
<blockquote>
  <pre>npp_save
cd $(CURRENT_DIRECTORY)
python -u $(FILE_NAME)</pre>
</blockquote>
<p>It runs most python code perfectly fine, I even tested out some of the example code that comes with python. Files using the turtle module work perfectly with opening windows etc.</p>
<p>However, when I run code using the Pygame module, everything works fine <em>except that no window is opened</em>. I see that the code is executing and I can print statements in the game loop and they show up in the console, but that is all.</p>
<p>Eventually I was able to get it to work. The issue was that NppExec was somehow blocking window creation in the case of Pygame. Using &quot;cmd /c python&quot; fixed the issue with the window:</p>
<blockquote>
  <pre>npp_save
cd &quot;$(CURRENT_DIRECTORY)&quot;
cmd /c python -u &quot;$(FILE_NAME)&quot;</pre>
</blockquote>
<p>Here is the related comment from superuser.com ( <code>https://superuser.com/questions/381942/stop-nppexec-from-trapping-console-output-until-program-finishes</code> ) where I found the solution to this problem:</p>
<p><i>To prevent your python script's own windows being suppressed (not sure exactly what is going on here), run the call to Python as an argument to cmd by preprending &quot;cmd /c&quot;. For example, I'm using</i></p>
<blockquote>
  <pre>cmd /c python -u &quot;$(FULL_CURRENT_PATH)&quot;</pre>
</blockquote>
<p><i>so my Cocos2D window shows up.</i></p>

<blockquote>
  <pre> </pre>
</blockquote>

</BODY>
</HTML>
