Fellow human beings!
I have recently installed pygame, following the awesome guide made by Bryson Payne that you can find here . I installed all of the following dependencies and programs, as per the order in the instructions.
Installed XQuartz
Homebrew was already installed (it was the second step).
Installed a version of python 3 from homebrew especifically for pygame use
Executed the following commands into the terminal: brew install mercurial, brew install sdl sdl_image sdl_mixer sdl_ttf portmidi, brew tap homebrew/headonly (this command failed, but pygame still let me download it! I read that if I did not have this, it should not let me download, so I just brushed it off. If you think this is the problem, tell me!), and, finally, brew install smpeg
Then, sudo pip3 install hg+http://bitbucket.org/pygame/pygame. This did not work on the first run, so I then installed it via 'pip3 install pygame', and it worked. I then tried the original command again, and it did download this time.
Additionally, I installed pgzero afterwards via the terminal, and then to pycharm via the project interpreter. The same was done to the normal pygame. And, something peculiar happened. The IDLE that was supposedly pygame enabled (python 3.7) actually did not work. I then tried to use the other IDLE I had before, 3.8.2, and that did work.
In the first try, which was a while ago (around 2 weeks), I really did not think much of the installation, since it had downloaded correctly. Then, when I tried to run my simple program on both PyCharm and IDLE, none worked! The code on IDLE displayed no error message... IDLE just runned the code but nothing happened, as python started running the program: but it appeared after a couple minutes, that the code was doing nothing. The following window, which executes the programs, appeared.
The window that executes programs
But PyCharm, running the same code, displays a different message. It goes as follows (notice that in the place above the .py file, there is the photo that was to be used).
See the image here
The new window did not show a >>> prompt, even after letting it run for various minutes. Python Launcher bounced on the dock, as if loading, until it stopped after 3 minutes.
The python launcher bouncing
So, to clarify, the code ran with no errors but did not display, even a basic window!
This happened with various small programs. The following are some examples.
import pygame
background_colour = (255,255,255)
(width, height) = (300, 200)
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Tutorial 1')
screen.fill(background_colour)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
I also ran some more complex programs, like the ones in the book 'Mission Python'. You can download the game files here.
Another small program I ran was:
import pygame
pygame.init()
img =pygame.image.load('astronaut.png')
display = pygame.display.set_mode((400, 400))
display.blit(img, [0, 0])
pygame.display.update()
The final one was...
import pygame as pg
import sys
pg.init()
screen = pg.display.set_mode((800,600))
done = False
while not done:
for event in pg.event.get():
if event.type == pg.QUIT:
done = True
pg.display.update()
pg.quit()
sys.exit()
After these failures (the program was not running), I uninstalled and then installed pygame and pgzero (in both IDLE and PyCharm) again. 3 times (none of those worked). After each install, I restarted the computer. Heck! I even tried to install by various methods (like sudo pip3 install pygame, pip3 install pygame, sudo pip install pygame, and finally, sudo pip3 instal pygame). When I tried to instal it using pip3, it installed pygame, and when I tried using only pip, it also installed pygame (this was done one command after the other).
I ask for help, because all of my development software/IDEs are pygame-enabled (after all, they don't show any error message). So what could it be? The code is copied from the pygame docs, that suggest using some of those snippets of code to prove pygame is working correctly. Really, I am desperate, since my goal is just to make pygame work.
Is it my usage of images incorrectly? But I am storing the files in the same directory as the images (same folder). I call my images as per my book (this is only applicable to the longer program found in the Mission Python files linked previously)
Remember, the main issue is that pygame is enabled but does not display. The text before is just some of the things I did to potentially solve this issue.
Is it the brew tap homebrew/headonly command that did not work? What else can I do to replace this command and get pygame to work?
There could be many reasons, so I'll tackle the one's I can think of.
Your code exits immediately.
When running the following code (or similarly structured codes),
import pygame
pygame.init()
img =pygame.image.load('astronaut.png')
display = pygame.display.set_mode((400, 400))
display.blit(img, [0, 0])
pygame.display.update()
the code will exit immediately, so you'll never notice the game actually running. It'll only run for a fraction of a second before it terminates. You must have some form of loop for it to keep open. This seems to be the problem in most of your examples (for example when you ran through IDLE).
I'd suggest you try with this code
import pygame
pygame.init()
screen = pygame.display.set_mode((800, 600))
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill((255, 0, 0))
pygame.display.update()
You should see a red window.
Your images is in the wrong path or have wrong names.
According to one of your pictures, you cannot load an image. This means that the program is working, but it cannot locate the picture rescue_ship.png. When running from an IDE, all files can be accessed by relative path that is relative to the project root.
I don't know what that is in your case, but I'd suggest that you try to use the absolute path instead, just to see if it's working. It could also be possible that you misspelled your image name; it has to be exact.
You're wrong python version.
Make sure you're not using Python 3.8 by accident. I don't think pygame supports it and I've seen many other questions that have problems using it. This might be difficult to notice since everything will work fine until it suddenly doesn't.
Also, make sure you're using the Python version that has pygame installed. For example, I have Python 3.5, 3.6 and 3.8, but only Python 3.5 has pygame installed. So if I run my pygame scripts with Python 3.6, it won't work (it'll complain with "No module named pygame").
Related
I have a program in a directory, which is started with a Main.py more or less as follows
import multiprocessing
def Procmatch(x):
Do something
if __name__ == '__main__':
print('Program has started')
Do some thing
p=multiprocessing.Process(target=Procmatch,args=(something,))
As I read, it can't be launched from inside the spyder IDLE because multi processes will stall, so I always launched it from the Anaconda Prompt.
Problem is that today that stopped working.
I do as usual, I open the prompt, navigated with cd somewhere/somewhere to the directory and then
(base) C:\somewhere/project diretory> python3 Main.py start
Instead of laucnhing it, as it used to, it just do nothing, and I get another
(base) C:\somewhere/project diretory>
Not even the print that shows it has started. Why is this happening?
I figured out what happened, so I'll just answer my question.
The problem is that I don't have more than one version of python installed, meaning there is no need to make a difference between python3 and python2. If I just type python Main.py start it does work properly.
My bad, as I was using the command for the raspberry pi
On a side note, and always keeping on mind nobody has any obligation towards anybody, I'm finding the community here is less willing to help with each passing year. Might be my biased view, but that is what I'm seeing.
I'm teaching myself python and have been exclusively been using Jupyter Notebooks through Anaconda until now. I'm now trying to move away from Jupyter and write .py scripts in an IDE. While working in Jupyter i've pip installed modules and they've worked fine within Jupyter. I've just discovered though that if i do this through an IDE i get a ModuleNotFoundError.
I've looked online and there are alot of posts with similar issues but none that quite matches mine. While i'm learning actual python code ok there is a huge hole in my knowledge around the setup, the terminal etc, and when i first started out i downloaded different IDE's which may not be helping...
Here's a few things from posts that i've tried that may give a clue as to what's wrong.
1) which python gives me //anaconda3/bin/python
2) which pip gives me //anaconda3/bin/pip
3) in python in the terminal:
import sys
for p in sys.path:
print(p)
//anaconda3/lib/python37.zip
//anaconda3/lib/python3.7
//anaconda3/lib/python3.7/lib-dynload
//anaconda3/lib/python3.7/site-packages
//anaconda3/lib/python3.7/site-packages/aeosa
4) usr/local/bin/ has a bunch of files (not folders) in it like 'Python3.7', 'Python3.7-config' etc
5) which -a pip = //anaconda3/bin/pip
6) which -a python gives me two paths = //anaconda3/bin/python
/usr/bin/python
7) usr/bin/python is a unix executable file, when i click it it opens a python terminal that says Python 2.7.16. within the usr/bin directory all the other python related files seem to reference 2.7.
8) when i pip install i generally just go 'pip install x' at the terminal. I thought maybe i needed to do 'pip3 install x' it would maybe not point just to anaconda, but was a complete guess and while the modules downloaded it didn't help at all.
9) I tried using an alias alias python=/usr/local/bin/python3 - but as python3 isn't actually there, i just created another problem that i then had to 'unalias'
10) pip -V = pip 19.1.1 from //anaconda3/lib/python3.7/site-packages/pip (python 3.7)
11) pip freeze showed the all the modules i would expect to see
This post seemed similar: Modules are installed using pip on OSX but not found when importing
Any help would be greatly, greatly appreciated. I've been learning ok up until now but i'm pretty out of my depth with this side of things, and i can't move forward at all unless i find a way to use an IDE with third party modules...
UPDATE - Tried uninstalling and reinstalling anaconda and it made no difference. All still works in Jupyter, modules can't be found elsewhere from IDEs. Also can't seem to install things like pandas through anaconda as it already has it, so it doesn't seem related to the IDE not being able to find it –
Thanks
When using the Anaconda distribution you should avoid pip as much as possible. Use the conda package manager instead. Try installing missing packages from the conda-forge channel first:
conda install newpackage -c conda-forge
Before you can use Anaconda properly, you need to activate it:
conda activate
The Jupyter start link did this for you. The Anaconda Prompt does the same thing and you can start your IDE directly from there. However, all IDE's manage environments themselfs, which is way more flexible. So it is time well spent to learn your IDE's features first.
I'm a beginner, I have really hit a brick wall, and would greatly appreciate any advice someone more advanced can offer.
I have been having a number of extremely frustrating issues the past few days, which I have been round and round google trying to solve, tried all sorts of things to no avail.
Problem 1)
I can't import pygame in Idle with the error:
ModuleNotFoundError: No module named 'pygame' - even though it is definitely installed, as in terminal, if I ask pip3 to install pygame it says:
Requirement already satisfied: pygame in /usr/local/lib/python3.7/site-packages (1.9.4)
I think there may be a problem with several conflicting versions of python on my computer, as when i type sys.path in Idle (which by the way displays Python 3.7.2 ) the following are listed:
'/Users/myname/Documents', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python37.zip', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/lib-dynload', '/Users/myname/Library/Python/3.7/lib/python/site-packages', '/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages'
So am I right in thinking pygame is in the python3.7/sitepackages version, and this is why idle won't import it? I don't know I'm just trying to make sense of this. I have absoloutely no clue how to solve this,"re-set the path" or whatever. I don't even know how to find all of these versions of python as only one appears in my applications folder, the rest are elsewhere?
Problem 2)
Apparently there should be a python 2.7 system version installed on every mac system which is vital to the running of python regardless of the developing environment you use. Yet all of my versions of python seem to be in the library/downloaded versions. Does this mean my system version of python is gone? I have put the computer in recovery mode today and done a reinstall of the macOS mojave system today, so shouldn't any possible lost version of python 2.7 be back on the system now?
Problem 3)
When I go to terminal, frequently every command I type is 'not found'.
I have sometimes found a temporary solution is typing:
export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin"
but the problems always return!
As I say I also did a system reinstall today but that has helped none!
Can anybody please help me with these queries? I am really at the end of my tether and quite lost, forgive my programming ignorance please. Many thanks.
Try it with the problem1
I'm not an expert neither, but I think you need to install both in terminal and python in order to use the program.
python -m pip install pygame
You should actually add the export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin" to your .bash_profile (if you are using bash). Do this by opening your terminal, verifying that it says "bash" at the top. If it doesn't, you may have a .zprofile instead. Type ls -al and it will list all the invisible files. If you have .bash_profile listed, use that one. If you have .zprofile, use that.
Type nano .bash_profile to open and edit the profile and add the command to the end of it. This will permanently add the path to your profile after you restart the terminal.
Use ^X to exit nano and type Y to save your changes. Then you can check that it works when you try to run the program from IDLE.
EDIT Replying to Rohan's comment here...
So I've tried import sys and sys.path for both in IDLE and in the terminal and the paths are different, not sure if that's where the problem is.
The code I'm trying to run is...
import pygame
pygame.init()
screen = pygame.display.set_mode((640, 480))
screen.fill((0, 255, 0))
pygame.display.flip()
pygame.time.wait(3000)
If I run the code in a terminal it works like it's supposed to, but I try it in IDLE it gets stuck on line with the Module Error.
CLOSE EDIT
I have searched through the already asked questions but couldn't find a solution to the issue I'm having. I have IDLE installed with Python 3.6.3.(if I type python3 in a terminal though I get Python 3.6.5) I've installed pip3 and the module pygame. However when I run my code I get the Module Not Found Error: No module named pygame.
If I run the code in the terminal directory everything works fine. So I thought I'd try another text editor Geany. After following all the instructions I can't even get Hello World to run on that. A terminal opens but freezes. The Compile and Execute lines were written exactly as they were supposed to be. I've tried updating and restarting my laptop but nothing works.
I'm just starting and following along with a couple books and everything has been going fine until this point but now I can't figure out what's going on. The only thing I can think of is IDLE only recognizes python 3.6.3, but if I type python3 into a terminal it says Python 3.6.5. I've tried updating and reinstalling IDLE but nothing seems to work. I can't even remove it. I haven't had any problems with code in IDLE until now and it's driving me crazy. Is there any way I can just reinstall everything and start it over fresh?
I apologize if this isn't written properly, it's my first post and the little guide on the right was covered up with some similar questions that aren't similar as I was reading it...
Question with no answers, but issue solved in the comments (…)
Answer from #Terry Jan Reedy in the comments:
It is unusual to have both 3.6.3 and 3.6.5 separately installed. If
pygame is installed for 3.6.5 (but not 3.6.3) and python3 in the
terminal starts 3.6.5, then python3 -m idlelib should start IDLE with
3.6.5 and code run with IDLE so started should be able to import pygame.
If the name of the file is pygame.py:
This file will be read/reached and NOT the pygame extension.
I have python3.4.0 installed on my system.
Today, I want to install python3.4.3, so I download the source code and install it.
However, my idle is still python3.4.0
while when I type python3 in terminal, it shows python3.4.3.
I also have pandas installed on my old version, it still can be used on my idle (linked with 3.4.0) but not with python3.4.3.
My question is how I can just sticked to python3.4.3 and make everything run in it.
I asked some questions in a comments. However, starting Idle with python3 -m idlelib will start Idle with whatever python is started with python3. Since you say that python3 starts 3.4.3, the above should run Idle 3.4.3.