After updating Python 2.7 to Python 3, Sublime won't recognize previously imported modules

443 Views Asked by At

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?

1

There are 1 best solutions below

3
On BEST ANSWER

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.