2

System information: Sublime Text 3.1.1 and Mac 10.14 Mojave

I am very new to programming and am just following a book to learn Python. I just updated from python 2.7 to python 3. Now, Sublime will not recognize modules I previously imported and worked with, namely pygame and pygal. When I try to import them, I get the following error:

import pygame

Traceback (most recent call last):
  File "/Users/MyNameHere/Desktop/python_work/data_visualization/python_repos.py", line 1, in <module>
    import pygame
ModuleNotFoundError: No module named 'pygame'

(where python_repos.py is the current program I am working in).

I know I have already installed these modules, because when I try to install pygame, for example, with pip install pygame in terminal, I get:

`Requirement already satisfied: pygame in /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages (1.9.4)`

Same goes with the other modules in question. I have tried doing many things to find a solution. One possible thing that comes up a lot is the suggestion to make sure sublime is using the updated version of pygame/pygal/etc. but I do not know how to do this.

Additionally, I am thinking maybe pygame/pygal/etc. are downloaded in some folder containing all my python 2.7 programs and are not with my Python 3 stuff. If this is correct, how would I go about adding the already downloaded programs to my Python 3 directory or whatever?

halfer
  • 19,824
  • 17
  • 99
  • 186
Sam
  • 81
  • 4
  • FYI there are some breaking changes from Python 2.7 to Python 3.0. So, if your book was written for Python 2, some things in it will be different for 3. You should definitely be using 3, so just be aware there are changes and check the migration guide at python.org if something from the book doesn't work. – A. L. Flanagan Nov 06 '18 at 20:29
  • Possibly your problem is related to the fact that `python` executes Python 2.# and `python3` is used to execute Python 3.# (because as @A.L.Flanagan mentioned, Python 2 and 3 are not 100% cross compatible). That generally means that they both store their libs in different places, you need `pip3` to install modules for Python 3, and also the built in build system for Python in Sublime executes `python`, so always version 2, requiring you to modify it to call `python3` instead (but it sounds like maybe you did that already). – OdatNurd Nov 06 '18 at 21:35

1 Answers1

2

You have to (re)install the packages you installed once to Python 2.x to Python 3.x. You can not use them to the other version. All the packages (mostly all) have the version to the specific Python version. It is the clearest way to get a reliable Python environment. Actually sometimes have 2.x version and vica versa which an experienced programmer can update him/herself, but this need is relatively rare and as you said this is not the case for sure.

So all you have to do is installing the actual packages' Python 3.x version to work with.

Additionally there are migration tools between the two main versions, but in this case I think it is unnecessary.

For example here you can find Pygame version for Python 3.x.

In the first case, when you tried to install Pygame, it was in the Python 2.x environment, that's because you get the message, that the requirements already satisfied, since as you wrote, you installed it already.

In contrary the error message occurred in the Python 3.x environment. So you have to aware of where you're installing the required package. A checkpoint for this, is run python --version in the command shell and read the message you've got, like:

Python 3.6.5 :: Anaconda, Inc

Though on some system with some installation you will find that python will start Python 3.x and on others will start Python 2.x and python3 will start Python 3.x. Similar can happen with pip/pip3 also. If you have a pip3 command or simlink it will handle packages for Python 3.x and so on (See my note below). Then you will know, which environment you're trying to install the given package.

Note:

The best way -and the most preferable as well- for using an another Python version then your system's default one using a Python virtual environment leaving your system default Python version as it is.

Geeocode
  • 5,705
  • 3
  • 20
  • 34
  • You are literally a god. Geeocode thank you so much. Helped me so well. Additionally, for anyone looking at this with the same problem, I originally didn't know how to download packages in terminal with python 3 that had the form "pip install _____". So basically, you just do "python3 -m pip install -U ______ --user" where the "_______" is the name of the package you want to install. Again, THANK YOU MY FRIEND – Sam Nov 06 '18 at 21:10
  • 1
    @Sam I don't know how under Mac goes, but you have to aware of which pip version you use. In some system there are two version of pip e.g. pip and pip3. Furthermore see edit above. Anyway my pleasure! – Geeocode Nov 06 '18 at 21:22
  • 1
    That is a better solution than my workaround. Yet again, you have proven your brilliance Geeocode. Cheers – Sam Nov 06 '18 at 21:44