Beginner unable to run pygame - python

I have just begun using pygame and am struggling to get anything running at all, I have reduced my code to these few lines:
import pygame
pygame.init()
size = (700, 500)
screen = pygame.display.set_mode(size)
but any code taken from working projects fails just as easily
Running this through shell causes it it to briefly open before crashing which from research IS normal?
But running it through the Python icon causes the prompt to open and close in an infinitesimal amount of time, I really don't know much about prompts so maybe it needs to be reconfigured or the PATH altered?
From research I've found that I'm meant to use a batch file but this just produces the error:
Attribute Error: 'module' object has no attribute 'display'
I'm sure my problem is straightforward and repetitive but I'm struggling to work out where even to begin in looking for advice myself, any helps appreciated thanks!

pygame.display is a module on its own, so you need to import it:
import pygame
import pygame.display
If you just import pygame, you are importing the pygame package (or to be more exact, the __init__.py file inside that package's folder). And while some package import all their submodules in that file, it is nothing you can assume/expect all packages to do. Therefor it's better to explicitly import the modules inside the package instead of just importing the top-level package.
Another possibility: Is your file called pygame.py by any chance? If yes, either rename it or add from __future__ import absolute_import to the top of your file.

That looks like a major corruption in your Pygame installation. Remove it, if necessary re-download, then re-install and try again.
Also make sure that you did not use any file name like "pygame.py" for your files. It would cause namespace-conflicts. So if you name your file "pygame.py" and then do "import pygame", it's basically your file being imported and I bet, you don't have a "display" class in there...
I used precisely the lines you have posted in your question and they run perfectly (Python 3.4, PyGame 1.9.2 on WinXP).

I just use
import pygame
from pygame import *
Works fine.

Related

How to fix my non-working pygame library?

Problem is whenever I try to run any python file with import pygame in it, it does not work. It always results in a ModuleNotFoundError: No module named 'pygame', though I do have pygame library installed.
I don't know if this is part of the problem but my pygame package is for some reason in \Lib\site-packages.

`import pygame` shows no errors even when pygame is not installed

I've been installing, uninstalling, and reinstalling pygame because there's a program that I've been trying to run (that uses pygame). However whenever I've installed pygame, the program doesn't work.
Traceback (most recent call last):
File "C:\Users\Ryan\Desktop\en\snake.py", line 28, in <module>
class Segment(pygame.sprite.Sprite):
AttributeError: 'module' object has no attribute 'sprite'
(Pygame is not installed when I receive this error supposedly)
What's really confusing me though is that when I type import pygame in normally in IDLE, I get no errors even when I just uninstalled it.
Windows 8 64 bit
Python 3.4.3
Pygame-1.9.2a0.win32-py3.4.msi (pygame file name)
Python is installed on C Drive (C:\Python34)
Whenever Pygame was installed, it was on the D drive
Have Python directory added to Path
Here's a screenshot of a video tutorial I was following, and it shows there are two installation paths. And here's mine, which only shows one path. I'm not sure if this is a big deal, but I just want to be sure.
Open your python interpreter and import pygame
Then you can print pygame to see exactly what is being loaded, should be something like: <module 'pygame' from '/Library/Python/2.7/site-packages/pygame/__init__.py'>
(repeat the above step until it fails importing pygame),
Close your prompt and install pygame. Try executing it again. Should the problem persist, you can edit some file in the game you are trying to run and add two lines: import sys and print sys.path and check the list of paths to make sure that there is not some other pygame installation somewhere bundled with the game that is being used instead of your installed pygame.
Modification of Josep Valls's answer:
You will need to change sys.path. This can be accomplished with the following code:
import sys
sys.path.append('''directory where pygame is''')
import pygame
The sys.path variable tell Python where to look for modules. By adding a path by append, you are telling Python to look for imported modules there. After that pointer is made, you can import pygame.

Including xlrd/xlwt/xlutils with modules outside of python installation

I'm self-taught in the Python world, so some of the structural conventions are still a little hazy to me. However, I've been getting very close to what I want to accomplish, but just ran into a larger problem.
Basically, I have a directory structure like this, which will sit outside of the normal python installation (this is to be distributed to people who should not have to know what a python installation is, but will have the one that comes standard with ArcGIS):
top_directory/
ArcToolbox.tbx
scripts/
ArcGIStool.py (script for the tool in the .tbx)
pythonmod/
__init__.py
general.py
xlrd/ (copied from my own python installation)
xlwt/ (copied from my own python installation)
xlutils/ (copied from my own python installation)
So, I like this directory structure, because all of the ArcGIStool.py scripts call functions within the pythonmod package (like those within general.py), and all of the general.py functions can call xlrd and xlwt functions with simple "import xlrd" statements. This means that if the user desired, he/she could just move the pythonmod folder to the python site-packages folder, and everything would run fine, even if xlrd/xlwt/xlutils are already installed.
THE PROBLEM:
Everything is great, until I try to use xlutils in general.py. Specifically, I need to "from xlutils.copy import copy". However, this sets off a cascade of import errors. One is that xlutils/copy.py uses "from xlutils.filter import process,XLRDReader,XLWTWriter". I solved this by modifying xlutils/copy.py like this:
try:
from xlutils.filter import process,XLRDReader,XLWTWriter
except ImportError:
from filter import process,XLRDReader,XLWTWriter
I thought this would work fine for other situations, but there are modules in the xlutils package that need to import xlrd. I tried following this advice, but when I use
try:
import xlrd
except ImportError:
import os, sys, imp
path = os.path.dirname(os.path.dirname(sys.argv[0]))
xlrd = imp.load_source("pythonmod.xlrd",os.path.join(path,"xlrd","__init__.py"))
I get a new import error: In xlrd/init.py, the info module is called (from xlrd/info.py), BUT when I use the above code, I get an error saying that the name "info" is not defined.
This leads me to believe that I don't really know what is going on, because I thought that when the init.py file was imported it would run just like normal and look within its containing folder for info.py. This does not seem to be the case, unfortunately.
Thanks for your interest, and any help would be greatly appreciated.
p.s. I don't want to have to modify the path variables, as I have no idea who will be using this toolset, and permissions are likely to be an issue, etc.
I realized I was using imp.load_source incorrectly. The correct syntax for what I wanted to do should have been:
imp.load_source("xlrd",os.path.join(path,"xlrd","__init__.py"))
In the end though, I ended up rewriting my code to not need xlutils at all, because I continued to have import errors that were causing many more problems than were worth dealing with.

How to install turtlegraphics in python

I'm trying my hands on Turtle but I can't get the module installed.
I have searched a lot and people seems to imply that it is included in the Python
standard library with Tkinter but this doesn't seem to be the case for me.
when I do:
import Tkinter
everything seems ok. But when I try
t1 = Turtle()
I get the error
NameError: name 'Turtle' is not defined
As per the tutorial I'm suppose to import like this:
from turtlegraphics import Turtle
But no matter what I've tried I cant find how I can get the library installed.
You need to import it from the turtle module.
from turtle import Turtle
t1 = Turtle()
t1.forward(100)
Try to check within the library files whether there is a file called turtle in it . I had the same problem and i found the library file and opened it. So check it and see if it will fix your problem. Most probably turtle library file will be in Lib folder.
Nothing here worked for me. This is what I did: I checked what configuration I had (in the upper right hand corner). I changed it so it was the python version that ran through the project and not just plain python. Then you also have to go to script path (also a setting in the configuation) and set it to the .py that you are working on :)
hey all of you guys just check importing that module ,you dont get any errors its already in standard libraray

How to import win32api

I'm trying to use some python-2.1 code to control another program (ArcGIS). The version of python I am using is 2.5. I am getting the following error message when I run the code.
<type'exceptions.ImportError'>: No module named win32api
Failed to execute (polyline2geonetwork2).
I tried installing pywin32-214.win32-py2.5.exe but I still get the same error message. I can't figure out if I need to do anything to my original python install so it knows that I have installed this.
I think the problematic part of my code is the following:
import win32com.client, sys, string, os, re, time, math
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
conn = win32com.client.Dispatch(r'ADODB.Connection')
Thanks for your help - I am quite new to python.
Your sys.path is
['C:\\Documents and Settings\\david\\My Documents\\GIS_References\\public\\funconn_public', 'C:\\Python25\\Lib\\idlelib', 'C:\\Program Files\\ArcGIS\\bin', 'C:\\WINDOWS\\system32\\python25.zip', 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\\site-packages', 'C:\\Python25\\lib\\site-packages\\win32', 'C:\\Python25\\lib\\site-packages\\win32\\lib', 'C:\\Python25\\lib\\site-packages\\Pythonwin']
and winapi.py is located in C:\Python25\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp.
Notice that this directory is not listed in your sys.path. To get things working, you'll need to put C:\Python25\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp in your sys.path.
It appears winapi.py is not yet installed. It is in a test\build...\temp directory.
I don't know much about Windows+Python. Maybe there is documentation that came with winapi.py which explains how the installation is suppose to be achieved.
A quick (but ugly) fix is to manually insert the needed directory into sys.path.
By this I mean, you can edit polyline2geonetwork.py and put
import sys
sys.path.append(r'C:\Python25\Lib\site-packages\isapi\test\build\bdist.win32\winexe\temp')
near the top of the file.
print out sys.path right before the import and make sure the path to win32com is in there

Categories