简体   繁体   English

即使正确安装,也无法在 Jupyter notebook 中导入 pygame

[英]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.

相关问题 即使安装了 pygame 也无法导入 - Can't import pygame even though it is installed 即使已经安装,也无法在 Jupyter 中导入 statsmodels? - Can't import statsmodels in Jupyter even though it is already installed? 即使应该安装 pygame 也无法导入 - Unable to import pygame even though it should be installed 无法在 jupyter 笔记本上导入袖扣和 plotly,即使它已经安装在 cmd 上 - Could not import cufflinks and plotly on jupyter notebook even though it has already been installed on cmd 无法在 jupyter notebook 中导入已安装的软件包 - Can't import installed packages in jupyter notebook 从终端运行脚本时无法导入 tensorflow,即使 tensorflow 在 jupyter 笔记本和终端中工作 - Can't import tensorflow when run script from terminal, even though tensorflow works in jupyter notebook and terminal 即使已安装TensorFlow和Keras,也可以在Jupyter中导入它们吗? - Can import TensorFlow and Keras in Jupyter, even though I have them installed? 即使安装了 PySerial 也无法导入 - Can not import PySerial even though it's installed Python 无法导入 SharePlum,即使安装正确 - Python fails to import SharePlum, even though it is installed correctly Python:已安装Anaconda,但无法在Jupyter笔记本中导入numpy或matplotlib - Python: Installed Anaconda, but can't import numpy or matplotlib in Jupyter notebook
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM