Search code examples
python-3.xpygamesublimetext3

easy SublimeText question regarding pygame module


I've been using SublimeText on OSX without issue... until I tried to import pygame. A simple few lines of code like:

print("Hello")
import sys
import pygame

gets the following output:

    Hello
Traceback (most recent call last):
  File "/Users/andrewjmiller/Desktop/python_work/new_file.py", line 5, in <module>
    import pygame
ImportError: No module named pygame
[Finished in 0.0s with exit code 1]
[shell_cmd: python -u "/Users/andrewjmiller/Desktop/python_work/new_file.py"]
[dir: /Users/andrewjmiller/Desktop/python_work]
[path: /Library/Frameworks/Python.framework/Versions/3.8/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Users/andrewjmiller/anaconda3/bin:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin]

But... I do have pygame installed, as evidenced here:

iMac:~ AndyTheAdmin$ python3 -m pip install --user pygame==2.0.0.dev6
Requirement already satisfied: pygame==2.0.0.dev6 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (2.0.0.dev6)

I've uninstalled and reinstalled SublimeText3. What should I try next?


Solution

  • You're falling victim to the difference between Python 2 and Python 3. In particular, note the line in the Sublime diagnostic output that says what command it was that failed:

    [shell_cmd: python -u "/Users/andrewjmiller/Desktop/python_work/new_file.py"]
    

    The Python.sublime-build file that ships with Sublime tells the OS to execute the command python.

    In your example where it works (or rather where you get confirmation that the library is installed):

    iMac:~ AndyTheAdmin$ python3 -m pip install --user pygame==2.0.0.dev6
    

    Here you're executing python3. Dollars to donuts if you were to type python --version in the terminal, it's Python 2 that Sublime is executing. The different versions of Python keep their packages in different locations, which is why it works in one place and not the other.

    Assuming that this is the case, the solution is to use a build that executes python3 instead. If you're already using a custom build system, then you can modify it directly. If you're using the build that ships with Sublime, the easiest way to make a new build is:

    1. Use View Package File from the command palette and open Python/Python.sublime-build to see what the existing build system looks like, and copy the entire thing to the clipboard.

    2. Use Tools > Build System > New Build System, replace the content with what you copied above, then swap the python for python3 in the two shell_cmd lines so that they execute the version that you expect.

    3. Save the file in the location that Sublime defaults to (should be your User package) as a sublime-build file with an appropriate name.

    Once you save, the Build system will be made available. You can either select it from Tools > Build System to use it directly, or if you have the build system in that menu set to Automatic the next time you do a build Sublime should prompt you to pick the build to use since there is now new builds.