0

I am installing pygame via macports. I followed the instructions as such,

sudo port install py26-game

Wrote the pygame code as such:

#image settings
bif = "bg.jpg"
mif = "ball.jpg"

#Basic Settings for pygame
import pygame, sys
from pygame.locals import *

pygame.init()

#Create a screen and load the images
screen = pygame.display.set_mode((640,360),0,32) #size,flag,bit

background = pygame.image.load(bif).convert()
mouse_c = pygame.image.load(mif).convert_alpha()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    screen.blit(background,(0,0))

    x,y = pygame.mouse.get_pos()
    x -= mouse_c.get_width()/2
    y -= mouse_c.get_height()/2

    screen.blit(mouse_c,(x,y))

    pygame.display.update()

I am using sublime text 2 to build the game, but I have this error:

Traceback (most recent call last):
  File "PygameTutorial.py", line 6, in <module>
    import pygame, sys
ImportError: No module named pygame

In my terminal, tried the same and have the same error:

>>> import pygame
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named pygame

How to solve this?

Community
  • 1
  • 1
lakshmen
  • 28,346
  • 66
  • 178
  • 276

2 Answers2

1

You need to ensure that you're using the MacPorts version of Python, and not the built-in one. Assuming your MacPorts installation is in /opt/local, you'll need to do the following:

  1. Edit your ~/.profile and include this line at the very bottom:

    export PATH=/opt/local/bin:$PATH
    

    Restart your Terminal session, run python on the command line, and see if you can import pygame.

  2. If that works, open a new file in Sublime with JSON syntax and paste in the following contents:

    {
        "cmd": ["/opt/local/bin/python", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    

    Save the file in your Packages/User directory as Python2.sublime-build where Packages is the folder opened by selecting Sublime Text 2 -> Preferences -> Browse Packages... - it should be ~/Library/Application Support/Sublime Text 2/Packages. Then, when you want to build a Python project, select Tools -> Build System -> Python2 and hit B to build.

    Alternatively, if you want to use the Tools -> Build System -> Automatic setting, you can edit the original Python.sublime-build file. Open your Packages folder as above, go to the Python directory, and open Python.sublime-build in Sublime. Change the contents to those above (basically, just change "cmd": ["python", ... to "cmd": ["/opt/local/bin/python", ... and save. Please note that this only works with Sublime Text 2; if you're using ST3 you'll need to install PackageResourceViewer to extract the Python.sublime-build file from the zipped Python.sublime-package file.

MattDMo
  • 100,794
  • 21
  • 241
  • 231
0

Check if you have pygame on the "site-packages" folder in the directory :

Python27/lib/site-packages

Sometimes that name of the file that contains it is different from "pygame", if you find something like py26-game

try changing the import to that name.

Pablo Estrada
  • 3,182
  • 4
  • 30
  • 74