4.6.4. Running Python & wxPython

1. Interactive Python inside NppExec

Running "python -?" gives the following help information, in particular:

The text in italic exactly refers to what is stated in NppExec's Manual, sections 1.3 and 3.1 by "NppExec is not a console emulator".

So, by running "python -i -u" in NppExec's Console, you get the interactive Python inside Notepad++.

Here is an advanced example of NppExec's script to be used to test a Python's program interactively:

npp_console local -  // disable any output to the Console
npp_save  // save current file (a .py file is expected)
cd "$(CURRENT_DIRECTORY)"  // 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 "$(FILE_NAME)"  // run Python's program interactively
 

2. Python and UTF-8

If your python's program contains some non-ASCII characters, you can get the following error from Python:

SyntaxError: Non-ASCII character

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 https://peps.python.org/pep-0263/ , you just need to specify

# coding=utf-8

or

# -*- coding: utf-8 -*-

at the beginning of your python's program.

Another thing is to output something to console as UTF-8. In this case, you can get something similar from Python:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-2: character maps to <undefined>

To fix this last one, it's enough to specify the environment variable PYTHONIOENCODING by setting it to "utf-8". I.e.

// 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
 

3. Running Python scripts using wxPython

[the text below has been originally posted by cioma in NppExec's forum]

[ cioma: ]

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.

Some time ago I started using wxPython GUI library and faced problems with running such scripts from within NppExec.

If I run this (in NppExec prompt):

python -t -B -u "$(FULL_CURRENT_PATH)" 

...then GUI part of wxPython is not shown. I guess the reason is that wxPython requires a "real" console buffer and NppExec doesn't provide that.

If I just run script over NPP "Run" 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.

So I found this solution to work:

1. When creating wxPython application in the script make sure it's STDOUT is not redirected:

app = App(redirect=False) 

2. Run in NppExec:

cmd /C python -t -B -u "$(FULL_CURRENT_PATH)" 

Voila! We have both wxPython GUI running and its STDOUT redirected to NppExec.

[ DV: ]

Just one thing. For more details regarding runtime errors parsing (keyword: Highlight filters), refer to [4.7.4] and "help con_filter".

 

4. Running Pygame Code

[the text below has been originally posted by Ahmed A. in Notepad++'s forum]

I have NppExec set up with this script:

npp_save
cd $(CURRENT_DIRECTORY)
python -u $(FILE_NAME)

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.

However, when I run code using the Pygame module, everything works fine except that no window is opened. 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.

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 "cmd /c python" fixed the issue with the window:

npp_save
cd "$(CURRENT_DIRECTORY)"
cmd /c python -u "$(FILE_NAME)"

Here is the related comment from superuser.com ( https://superuser.com/questions/381942/stop-nppexec-from-trapping-console-output-until-program-finishes ) where I found the solution to this problem:

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 "cmd /c". For example, I'm using

cmd /c python -u "$(FULL_CURRENT_PATH)"

so my Cocos2D window shows up.