[英]Can not import pygame in Jupyter notebook even though it is installed correctly
I want to install pygame on my MacBook.我想在我的 MacBook 上安装 pygame。 I installed it with the command
我用命令安装了它
pip install -U pygame --user
However, whenever I ran import pygame
, I get ModuleNotFoundError
.但是,每当我运行
import pygame
,都会得到ModuleNotFoundError
。 I get the same error whether I ran it inside a Jupyter notebook or from the terminal.无论是在 Jupyter 笔记本中还是从终端运行它,我都会遇到同样的错误。
>>> 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.我通过检查
pip freeze
检查它是否安装正确,并且我运行了命令python3 -m pygame.tests
来检查它是否运行良好并且确实如此。 I added it to the system path as you can see below.我将它添加到系统路径中,如下所示。 The pygame folder is in the site-packages folder.
pygame 文件夹位于 site-packages 文件夹中。
(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
.感谢@Matiiss 的评论,我设法通过在终端中运行
python3
来导入模块,然后import pygame
。 However, it is tsill not working on Jupyter even though the kernel in the notebook is Python 3.但是,即使笔记本中的内核是 Python 3,它仍然无法在 Jupyter 上运行。
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.答案是我必须使用
python -m pip install -U pygame
而不是pip install pygame
以便 Jupyter 可以找到该模块。 I honestly do not know what is the difference, so maybe somebody can elaborate, but that's the solution for my problem.老实说,我不知道有什么区别,所以也许有人可以详细说明,但这是我问题的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.