简体   繁体   中英

Can not import pygame in Jupyter notebook even though it is installed correctly

I want to install pygame on my MacBook. I installed it with the command

pip install -U pygame --user

However, whenever I ran import pygame , I get ModuleNotFoundError . I get the same error whether I ran it inside a Jupyter notebook or from the terminal.

>>> import pygame
===== 2021.11.03 13:11:29 =====
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'pygame'

I uninstalled and re-installed it with different commands:

pip install -U pygame
pip3 install -U pygame --user
pip3 install -U pygame

I checked it is installed correctly by checking pip freeze and I ran the command python3 -m pygame.tests to check it works well and it does. I added it to the system path as you can see below. The pygame folder is in the site-packages folder.

(base) aelashry@Ahmeds-MacBook-Pro asn5 % echo $PATH
/Users/----/Library/Python/3.9/lib/python/site-packages:/Users/----/Library/Python/3.9/bin:/opt/homebrew/bin:/Users/----/opt/anaconda3/bin:/Users/----/opt/anaconda3/condabin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin

I also restarted my computer. Nothing works. Any body can help?

Edit

Thanks to @Matiiss comments, I managed to import the module by running python3 in the terminal and then import pygame . However, it is tsill not working on Jupyter even though the kernel in the notebook is Python 3.

The answer turned out that I had to use python -m pip install -U pygame instead of pip install pygame so that Jupyter can find the module. I honestly do not know what is the difference, so maybe somebody can elaborate, but that's the solution for my problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM