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.
Related
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").
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 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.
Ok, I am using python 3.4.3 and I think I downloaded the right file but when I go to python shell, it says No module named 'cx_Freeze'
I know there are plenty of questions like this but none of them helped. There was one I found using my exact same problem and version but even that did not work. I do not know what to do. I have put the file in the same place, I think anyways, as python is and I tried putting it on my desktop but still does not work. Any ideas?
faced a similar problem (Python 3.4 32-bit, on Windows 7 64-bit). After installation of cx_freeze, three files appeared in c:\Python34\Scripts:
cxfreeze
cxfreeze-postinstall
cxfreeze-quickstart
These files have no file extensions, but appear to be Python scripts. When you run python.exe cxfreeze-postinstall from the command prompt, two batch files are being created in the Python scripts directory:
cxfreeze.bat
cxfreeze-quickstart.bat
From that moment on, you should be able to run cx_freeze.
cx_freeze was installed using the provided win32 installer (cx_Freeze-4.3.3.win32-py3.4.exe). Installing it using pip gave exactly the same result.
Ok, I figured it out. So this is for all the future people have the same problem as I am. First, download pip. Then open a python shell and import pip. This is to make sure the download of pip was successful. Then go to the cx_Freeze website and for python 3.4.3, it will be the last one I think. It will say the version of cx_Freeze and then say the version of python which is 3.4.3 for me. That will download and then go to python shell and import cx_Freeze. It should work. Remember that you have to capitalize the "F" and have the code be exactly like this "cx_Freeze" but without the quotes. That is how I solved this problem with this exact python version.
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.