How do I run Pygame on Pycharm - python

I am trying to run pygame on the PyCharm IDE, I have installed the latest version of pygame for python 3.5 and have added it to the project interpreter. I installed pygame from http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygame and copied it too python35-32/Scripts/. The test program below runs fine in the python shell but I want to run it from PyCharm.
I have been trying to solve this for the past hour and I've hit a wall.
When I run this simple test program to see if pygame is working:
import pygame
pygame.init()
pygame.display.set_mode((800, 800))
I get this error:
Traceback (most recent call last):
File "C:/Users/jerem/PycharmProjects/Games/hello_pygame.py", line 1, in <module>
import pygame
File "C:\Users\jerem\AppData\Roaming\Python\Python35\site-packages\pygame\__init__.py", line 141, in <module>
from pygame.base import *
ImportError: No module named 'pygame.base'
Any help would be greatly appreiciated!
Thanks again,
JC

Follow the instructions provided here. I think its related to the problem you are having on pygame and PyCharm on windows.
How do I download Pygame for Python 3.5.1?

its bcus pycharme has not recognised you're env or working on wrong env
https://www.jetbrains.com/help/pycharm/creating-virtual-environment.html
check this

Related

Pygame on Raspberry Pi - no driver

I got a problem with setting up my Pygame code on RasPi 4. I have HDMI display (7" for now, later to be replaced by 4" on final product) connected to it (boot to console), but do all development on remote desktop.
When I use pygame 1.9, i have no problems using fbcon driver, and can show stuff on display. I now installed pygame_gui, and it pushed up the pygame version to 2.1.2. Now, I cant get the screen working, the error is
pygame 2.1.2 (SDL 2.0.14, Python 3.9.2)
Hello from the pygame community. https://www.pygame.org/contribute.html
Setting SDL variables...
...done
Checking current env variables...
SDL_FBDEV = /dev/fb0
Traceback (most recent call last):
File "/home/proto/pygameui.py", line 24, in <module>
pygame.display.init()
pygame.error: fbcon not available
I tried to set driver to fbcon, directfb, svgalib, kmsdrm, but in vain.
Trying to downgrade pygame and pygame_gui to 1.9 and 5.7 worked for normal pygame graphics, but I cant get gui stuff to work with this version..
Any idea what to do?
Found a solution in meanwhile, which is a bit bulky, but works.
I only edit from remote desktop, but start the python execution from a separate ssh console. This way, kmsdrm driver works.
When the raspi will be installed into a standalone hardware device, it would anyways autostart the python script.

How to doHeadless Pygame with SSH?

I am trying to create a python script with pygame to control servos with a gamepad. I want to be able to do this without a display of any kind. I am running the script on a Raspberry Pi 4 Model B with Raspbian OS and to run scripts I am using SSH from bash terminal (I've tried on VS Code terminal and git-bash terminal) on a windows computer with OS Windows 8. I've seen some work arounds for headless pygame code:
https://www.pygame.org/wiki/HeadlessNoWindowsNeeded
https://gist.github.com/illume/f2fcdde9478aa3f1db8f3ee17359cf1c
Pygame headless setup
Pygame display init on headless Raspberry Pi Zero (Raspbian Buster Lite)
Pygame on Pi running through Putty, no screen, no input
Pygame on Pi running through Putty, no screen, no input
However, none have worked and I keep getting these errors:
Traceback (most recent call last):
File "ps4ServoControl.py", line 15, in <module>
pygame.display.init()
pygame.error: Unable to open a console terminal
or
Traceback (most recent call last):
File "ps4ServoControl.py", line 63, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
I've tried every variation of the following lines:
os.environ["SDL_VIDEODRIVER"] = "dummy"
os.putenv('SDL_VIDEODRIVER', 'fbcon')
os.putenv('DISPLAY', ':0.0')
pygame.display.init()
screen = pygame.display.set_mode((1,1))
Is it just because I'm trying to do this with SSH? I'm not sure how else to test my scripts without doing a remote desktop, but again, I don't want to have to pull up a screen somewhere when I get the script working. Any help is appreciated. Thanks!
In my case, I was headless and using pygame for sounds. The code you showed used to work for me, but after updating this year I was getting the same error. I resolved it by commenting out the following lines:
pygame.display.init()
screen = pygame.display.set_mode((1,1))
Everything seems to be fine without them.

Python program throws error because it says it cannot find pygame even though I have imported it in the program

import sys
import pygame
def run_game():
pygame.init()
screen=pygame.display.set_mode((1200,800))
pygame.display.set_caption("Space Battle")
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.display.flip()
run_game()
I get this error:
Traceback (most recent call last):
File "C:/Users/Dell/PycharmProjects/test/test.py", line 3, in
import pygame
ModuleNotFoundError: No module named 'pygame'
I have installed pygame and did the import command on the cmd on windows.
How can I solve the problem?
If you have more than one python editor in your pc, make sure you installed pygame into the right python editor
This kind of problem arises because of two resiongs:
If you have more then on python version in your system and you don't set the python environment yet for default python version to use.
Some time editor not response well after installing packages or library. In this case just close the editor and restart it. Most of the time it helps to fetch the library.

Curses crash on execute (Python 3.7.2 on Windows 10)

Found out about Curses a couple days ago and wanted to start messing around with it to see what I could create.
I followed some instructions for installation on another thread and it installed just fine. However, when I attempt to run initscr() (which initializes the screen, it crashes)
Here's the Error text:
{Traceback (most recent call last):
File "C:\Users\ADimi\Desktop\Wing Workspace\test.py", line 2, in <module>
stdscr = curses.initscr()
File "C:\Users\ADimi\AppData\Local\Programs\Python\Python37\lib\curses\__init__.py", line 30, in initscr
fd=_sys.__stdout__.fileno())
AttributeError: 'NoneType' object has no attribute 'fileno'}
To recreate:
You can run this in either, idle or a seperate file. For me this directly causes the error, specficially line 2. stdscr = curses.initscr()
import curses
stdscr = curses.initscr()
From what I'm understanding from the error itself, _sys.__stdout__ is returning a NoneType and thus leading to a crash.
I tried to find similar issues online and have had no luck. Any help at this point could help out a lot I'd love to get this working.
Here's a link about curses:
https://docs.python.org/2/howto/curses.html#curses-howto
Here's where I found out about curses, the first comment is the installation instructions I followed:
What is needed for curses in Python 3.4 on Windows7?
Thanks in advance.
EDIT:
I have some what found a fix. There is a windows tailored version of curses found here: https://pypi.org/project/windows-curses/#files
After uninstalling the old curses and installing this you must follow some intermediate steps:
Create a file with this test code:
import curses
import time
screen = curses.initscr()
screen.clear()
time.sleep(4)
Create a path for 'python' for your cmd prompt:
https://www.pythoncentral.io/add-python-to-path-python-is-not-recognized-as-an-internal-or-external-command/
Now using the cmd prompt open the file:
python test.py
You should be presented with a black screen for 4 seconds, then it will close.
This is the only work around I've come across, I will keep searching and updating as I go thank you.

No module named pygame

Why won't this work? The best solution I could find is that the name of my game can't be pygame and I changed the name, yet it still doesn't work. I'm getting this error message:
Traceback (most recent call last):
File "C:\Users\dnw13\Desktop\Python projects and notes\pygame games.py", line 1, in <module>
import pygame, sys
File "C:\Users\dnw13\Desktop\Python projects and notes\pygame.py", line 2, in <module>
from pygame.locals import *
ModuleNotFoundError: No module named 'pygame.locals'; 'pygame' is not a package
here is my code
import pygame, sys
from pygame.locals import *
pygame.init()
DISPLAYSURF = pygame.display.set_mode((400, 300))
pygame.display.set_caption('Hello World!')
while True: # main game loop
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
First off, if you're going to use a library called pygame, you shouldn't also have a .py file named pygame.py in your working directory. Second, pygame, unlike tkinter, is not pre-installed the way tkinter was. You will need to install it separately using one of the following methods:
1st way, using pip:
# in the console (Terminal or cmd) depending on your OS, type:
# for windows: (replace 3 with 2 if using python 2)
py -3 -m pip install pygame
# or: (just pip for python 2)
pip3 install pygame
# for Debian: (Mac OS or Unix or Linux etc.)
pip3 install pygame
# just pip for python 2
# or: (replace 3 with 2 for python 2)
python3 -m install pygame
2nd way, from pre-compiled file:
Go to the official download page. And choose your operating system to download the file. You can also follow the official installation tutorial page.
3rd way, build from source:
Follow the same method from the second method and choose the source files. Then do ./configure then make && make install in the console. This method is not recommended.
If you work in Windows (I solved it in Win 10), look at global variables:
search for System Info
choose button Environment Variables...
look at the Path variable to see if Python has the right path
if not change it :)
if necessary - restart programm(s)
It happens when you have multiple versions of Python installed
Make sure pygame is added to the correct path. It installed pygame in the wrong place and python couldn't find it. It took it from where it had installed it (it tells you were this is when you install it.) and dragged it into where i save my games and it now works.

Categories