1

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'
jwpfox
  • 5,124
  • 11
  • 45
  • 42
  • Don't know how this works on Mac, but the problem is that PyGame (and any other package) must be installed for each Python individually. You've only installed it for Python 2. You need to _also_ install it for Python 3. – geometrian Jul 05 '16 at 22:34
  • You mentioned `virtual` environment. All you need is to activate the environment where `python2.7` is. Note that El Capitan also comes with its own python 2.7. Make sure you don't get confused one over the other. – alvits Jul 05 '16 at 22:37
  • Hi guys anyone can help me here ? http://stackoverflow.com/questions/38244537/bug-pyinstaller-payload-veil-show-prompt-windows-while-i-run-it – paolo rossi Jul 13 '16 at 17:18

1 Answers1

1

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.
Community
  • 1
  • 1
sudo
  • 5,604
  • 5
  • 40
  • 78
  • Ok. I followed your instructions. To do so I created another virtual environment and I installed pygame as `pip3 install hg+bitbucket.org/pygame/pygame`;. However to make `pyinstaller` working, I had to install it in my virtual env with `pip3 install pyinstaller` before running it. – Giacomo Carloni Jul 06 '16 at 09:42
  • Now I have another small problem. When I run the executable in the virtual environment it works properly, but in the moment I run in the local environment I have the following error `pygame.error: Couldn't open images/ProEuro1.png`. Does `pygame` includes the images used in the code? If not how do I manage them? – Giacomo Carloni Jul 06 '16 at 09:47
  • I'm not familiar with `pygame` or `pyinstaller` specifically, but judging by the error message, I don't think `pygame` contains the images in the source. There must be an images directory in your working directory somewhere. It needs to be in the same directory as wherever the Python file is being opened from. If you can find it, you can try moving it around, but beyond that, you'll probably need to look around SO more. – sudo Jul 06 '16 at 18:45