简体   繁体   English

错误:Python3 中 Jupyter 笔记本上的“导入 Pandas”失败

[英]Error: "Import Pandas" fails on Jupyter notebook in Python3

I am having an issue trying to import pandas on my Jupyter notebook using Python 3.10.我在尝试使用 Python 3.10 在我的 Jupyter 笔记本上导入熊猫时遇到问题。 I am using a MacOS computer with VSCode as my IDE.我正在使用带有 VSCode 作为我的 IDE 的 MacOS 计算机。

I have installed pandas on my machine using Homebrew brew install pandas and it works just fine when I use import pandas in my IDE.我已经使用 Homebrew brew install pandas pandas 在我的机器上安装了 pandas,当我在我的 IDE 中使用import pandas时它工作得很好。 However, it fails in Jupyter.但是,它在 Jupyter 中失败了。

I open jupyter as normal with jupyter notebook .我用jupyter notebook正常打开 jupyter 。 I then attempt the simple command import pandas I get an error message that says:然后我尝试简单的命令import pandas我收到一条错误消息,上面写着:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Input In [1], in <cell line: 1>()
----> 1 import pandas

ModuleNotFoundError: No module named 'pandas'

I did a quick search on Stack and across Google and everybody tells me to run !pip install pandas in the Jupyter notebook but since I am using PHP3.10, I have opted for !pip3 install pandas .我在 Stack 和 Google 上进行了快速搜索,每个人都告诉我在 Jupyter 笔记本中运行!pip install pandas但由于我使用的是 PHP3.10,所以我选择了!pip3 install pandas When I do this, Jupyter tells me:当我这样做时,Jupyter 告诉我:

Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pandas in /Users/daveshields/Library/Python/3.10/lib/python/site-packages (1.4.2)
Requirement already satisfied: python-dateutil>=2.8.1 in /Users/daveshields/Library/Python/3.10/lib/python/site-packages (from pandas) (2.8.2)
Requirement already satisfied: numpy>=1.21.0 in /Users/daveshields/Library/Python/3.10/lib/python/site-packages (from pandas) (1.22.4)
Requirement already satisfied: pytz>=2020.1 in /Users/daveshields/Library/Python/3.10/lib/python/site-packages (from pandas) (2022.1)
Requirement already satisfied: six>=1.5 in /Users/daveshields/Library/Python/3.10/lib/python/site-packages (from python-dateutil>=2.8.1->pandas) (1.16.0)

I close Jupyter and then open it again and attempt import pandas and I get the exact same message.我关闭 Jupyter,然后再次打开它并尝试import pandas ,我得到了完全相同的消息。 Can anyone help me figure out why this is happening?谁能帮我弄清楚为什么会这样? I am doing the Python Master Course by Ardit Sulce on Udemy but I can't progress any further in the course until I can figure this out.我正在 Udemy 上由 Ardit Sulce 开设的 Python 硕士课程,但在弄清楚这一点之前,我无法在课程中取得任何进展。 TYIA.蒂亚。

Dave Shields戴夫·希尔兹

You can use:您可以使用:

import sys   
!{sys.executable} -m pip install pandas

If you already have Pandas in your system and still not able to load it then :如果您的系统中已经有 Pandas 并且仍然无法加载它,那么:

import sys  
sys.path.append('your-path') 

Thank you so much to Wayne!非常感谢韦恩!

Amazingly the %pip install pandas worked!令人惊讶的是%pip install pandas起作用了! I did that and it installed pandas and numpy.我这样做了,它安装了 pandas 和 numpy。

I ran import pandas and then df1 = pandas.DataFrame([[2,4,6],[10,30,30]]) .我运行import pandas然后df1 = pandas.DataFrame([[2,4,6],[10,30,30]])

After that I was able to successfully use print(df1) and it worked!之后,我能够成功使用print(df1)并且它起作用了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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