Giacomo Carloni
Giacomo Carloni

Reputation: 25

Pygame + Pyinstaller = ImportError: No module named 'pygame'

I started to learn Python last week. I am newbie with not much coding experience.

I created a "Space Invader" game using Pygame (installed in my virtual environment) and it works perfectly when I launch it with Python2.7 as

python myfile.py

However if I launch it as

python3 myfile.py

I have a Traceback error

ImportError: No module named 'pygame'

Cool I can deal with it, even if I don't know why. However when I use pyinstaller myfile.py, it converts the program into stand-alone executables using Python3

115 INFO: PyInstaller: 3.2
115 INFO: Python: 3.5.0b4
125 INFO: Platform: Darwin-15.5.0-x86_64-i386-64bit
126 INFO: wrote mydir/myfile.py

and therefore the same error when I launch the app. I am working on a Mac OSX El Captain.

The questions are: Why it does not work on Python3? How do I use 'pyinstaller' with Python2.7?

All the explanations about package management with a "human" language are welcome.

P.S. I tried cx_Freeze and bbFreeze but I always end up with the following error. I have a Mac OSX El Captain.

OSError: [Errno 1] Operation not permitted: '/mydir/MacOS.so'

Upvotes: 1

Views: 3267

Answers (1)

sudo
sudo

Reputation: 5784

Python libraries are installed separately for Python 2 and Python 3. Looks like you have pygame installed only for Python 2. Regular pip install [package] only installs the Python 2 version in most setups. In general, everything considers Python 2 the "default" version for some reason, including the other major Unix-based OSs.

  1. First, activate your virtualenv (source ./bin/activate in the venv directory).

  2. pip3 install hg+http://bitbucket.org/pygame/pygame (or maybe pip-3.5) in your virtualenv should fix it.

  3. If it complains about hg not being found, install Mercurial somehow (e.g. sudo port install mercurial if you have MacPorts).

  4. Try it out. Run python3, then put import pygame.

Sources:

  • pygame installation from pip: Unable to install Pygame using pip
  • I'm using OS X El Capitan. I just tested my answer, and I'm able to open a Python 3 interpreter and import pygame.

Upvotes: 2

Related Questions