Notepad++ NPPEXEC Unable to Properly Run Pygame Code
-
I have nppexec set up with this command
npp_save cd $(FULL_CURRENT_PATH) 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.
The program itself works perfectly fine when run from the idle or in vscode, I am not sure what’s causing this issue.
For anyone who wants to test this, here is some simple code for pygame that initializes a window with a moving square.
import sys import pygame SPEED = 4 class Game: def __init__(self): pygame.init() pygame.display.set_caption('GAME') self.screen = pygame.display.set_mode((640,480)) self.clock = pygame.time.Clock() self.pos = [10, 10] self.x_vel = SPEED self.y_vel = SPEED def run(self): while True: self.screen.fill((30,30,36)) pygame.draw.rect( self.screen, (140, 230, 235), pygame.Rect(*self.pos, 50, 50) ) if self.pos[0] >= 640 - 50: self.x_vel = -SPEED elif self.pos[0] <= 0: self.x_vel = SPEED if self.pos[1] >= 480 - 50: self.y_vel = -SPEED elif self.pos[1] <= 0: self.y_vel = SPEED self.pos[0] += self.x_vel self.pos[1] += self.y_vel for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() pygame.display.update() self.clock.tick(60) Game().run()
If I try to run the code using F5 and put this into the “Program to run category”
C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\python.exe "$(FULL_CURRENT_PATH)"
The code runs fine, window opens and everything.
The issue is that I want have the NPPEXEC terminal for debugging purposes rather than have it act as if I just ran the code from file explorer or desktop.
-
@Ahmed-Abuharthieh said in Notepad++ NPPEXEC Unable to Properly Run Pygame Code:
I have nppexec set up with this command
npp_save cd $(FULL_CURRENT_PATH) python -u $(FILE_NAME)
…
If I try to run the code using F5 and put this into the “Program to run category”C:\Users\Ahmed\AppData\Local\Programs\Python\Python310\python.exe "$(FULL_CURRENT_PATH)"
Your two commands are different in at least three ways that I can see.
- The npp_exec uses
-u
and the Run command does not. I don’t know why buffering or not would mess pygame up, but I don’t know enough about pygame to comment on that specifically - The npp_exec does not put quotes around the filename (though if your filename had a space in it, I would think it wouldn’t run at all)
- You use the full path to python.exe in Run but not in npp_exec, so it may be that the python in the PATH that npp_exec can see is in a different directory than the one you run from the Run menu.
- The npp_exec uses
-
@PeterJones said in Notepad++ NPPEXEC Unable to Properly Run Pygame Code:
My current Script
npp_save
cd “$(FULL_CURRENT_PATH)”
python “$(FILE_NAME)”Thank you for your response
- I made adjustments to these lines so that there are quotes around the file paths
- Tried with and without the -u
- Tried putting in the full path to python
I get the same result, except when I don’t use -u, I get a large delay before it starts printing to terminal. But still no window opening.
-
Okay, 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
- using python -u makes it so that the console does not wait for the program to finish to start printing the output
here is the final command I used to get everything working well
npp_save cd "$(FULL_CURRENT_PATH)" cmd /c python -u "$(FILE_NAME)"
Here is a link to where I found the solution to these problems
-
Thank you for this information! Added to the NppExec Manual (the point “4. Running Pygame Code”) :
https://htmlpreview.github.io/?https://github.com/d0vgan/nppexec/blob/develop/NppExec/doc/NppExec/NppExec_Manual/4.6.4.html -
@Vitalii-Dovgan
Cool, I just checked out the link. Looks good!