导入pandas库时出现错误:ImportError: No module named pandas。

6

我似乎无法导入panda包。我使用Visual Studio代码编写。我使用的是Mac并且拥有OSX 10.14 Mojave。

我正在尝试编译的代码是:

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
house_data = pd.read_csv('house.csv')
plt.plot(house_data['surface'], house_data['loyer'], 'ro', markersize=4)
plt.show()

当我尝试在终端中使用pip install pandas时,出现以下情况:

(base)  pip install pandas
Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (0.24.0)
Requirement already satisfied: pytz>=2011k in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2018.9)
Requirement already satisfied: python-dateutil>=2.5.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2.7.5)
Requirement already satisfied: numpy>=1.12.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (1.15.3)
Requirement already satisfied: six>=1.5 in /Users/Library/Python/3.7/lib/python/site-packages (from python-dateutil>=2.5.0->pandas) (1.12.0)
(base) Thibaults-MBP-5d47:ML_folder thibaultmonsel$

当我执行我的代码时,我得到了以下结果:
Traceback (most recent call last):
  File "ML1.py", line 5, in <module>
    import pandas as pd
ImportError: No module named pandas

如果我尝试运行sudo pip install pandas,我会得到以下结果:

(base) MBP-5d47:ML_folder $ sudo pip3 install pandas --upgrade
Password:
The directory '/Users/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory.If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting pandas
  Downloading https://files.pythonhosted.org/packages/34/63/529fd1391044051514f2f22d61754245db2133cd37c4dad7150a1cbe2ece/pandas-0.24.1-cp37-cp37m-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl (15.9MB)
    100% |████████████████████████████████| 15.9MB 901kB/s
Requirement already satisfied, skipping upgrade: python-dateutil>=2.5.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2.7.5)
Requirement already satisfied, skipping upgrade: numpy>=1.12.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (1.15.3)
Requirement already satisfied, skipping upgrade: pytz>=2011k in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2018.9)
Requirement already satisfied, skipping upgrade: six>=1.5 in /Users/Library/Python/3.7/lib/python/site-packages (from python-dateutil>=2.5.0->pandas) (1.12.0)
Installing collected packages: pandas
  Found existing installation: pandas 0.24.0
    Uninstalling pandas-0.24.0:
      Successfully uninstalled pandas-0.24.0
Successfully installed pandas-0.24.1

然而,我仍然遇到了 no modules named pandas 的问题。

最后,当我尝试运行 pip3 install pandas 时,出现以下提示:

Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (0.24.0)
Requirement already satisfied: pytz>=2011k in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2018.9)
Requirement already satisfied: numpy>=1.12.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (1.15.3)
Requirement already satisfied: python-dateutil>=2.5.0 in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (from pandas) (2.7.5)
Requirement already satisfied: six>=1.5 in /Users/Library/Python/3.7/lib/python/site-packages (from python-dateutil>=2.5.0->pandas) (1.12.0)

当我尝试执行程序时,使用 pip3 install pandas 后出现与上述相同的错误...。
如果有帮助,我也进行了 import.sys
base)-MBP-5d47:ML_folder $ python help1.py
2.7.10 (default, Aug 17 2018, 17:41:52)
[GCC 4.2.1 Compatible Apple LLVM 10.0.0 (clang-1000.0.42)]

这是我的 sys.path

['/Users/Desktop/ML_folder', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

你安装了Pandas吗? - It_is_Chris
请整理您的输出,只显示与您的问题相关的部分。很可能您没有安装pandas。我猜想您可能正在使用一个名为'base'的虚拟环境?您需要在该环境中安装所有必要的软件包。 - PDiracDelta
这回答解决了你的问题吗?无法导入已确定已安装的模块 - Tomerikoo
9个回答

6
您需要使用以下命令安装pandaspip install pandas 如果您遇到特权问题,可能需要运行以下命令: sudo pip install pandas 在Python 3上,您可能需要运行以下命令: pip3 install pandas(尽管pip可能已经指向pip3)。您可以在此SO帖子中阅读有关pip版本之间的区别的信息。
如果您没有安装pip,请参见此处进行安装。

你好PJW,我修改了我的帖子。我尝试了你所有的建议,但似乎没有起作用。我在网上搜索了很多,但仍然没有答案... - Thibault
是的,看起来 Pandas 包被安装在与您的 Python 安装不同的路径中。如果您正在 base 虚拟环境中完成所有操作,则应该可以……不确定。 - pjw

1

对我来说,在MAC中以下命令可行

sudo -H pip3 install pandas --upgrade


0

在输入运行文件的命令时,请确保指定您正在使用的Python版本。例如,不要使用python filename.py,而是使用python3 filename.pypython2 filename.py


0

如果你在IDE中看到这样的内容,并且在运行代码时出现“没有找到pandas模块”的错误,那么这意味着尽管你已经执行了“pip install pandas”或其他操作,但pandas并未被安装。

前往文件 > 设置 > 项目解释器,查看pandas是否在列表中可用。如果不可用,只需点击+(加号),选择pandas并将其安装到您的项目环境中。
参见图片 然后等待IDE更新您的项目框架... voila,错误消失了!


0
代码 > 首选项 > 设置 在搜索中,键入“解释器” 你会看到一个条:Python:默认解释器路径 粘贴您的正确的 Python 路径(在 Mac 上类似于“/usr/local/bin/python3”),它将自动保存 然后返回到您的 python 文件并尝试运行

0

在项目外安装pandas,我想仅为env环境下载它,但是我遇到了相同的错误,所以我从外部进行了安装。


0

通过以下命令检查您的环境中pandas包的路径:
jupyter kernelspec list

如果您看到路径为: /Users/yourname/Library/Jupyter/kernels/yourenv

请删除Library文件夹中的Jupyter文件夹,然后重新运行。


0

检查您的虚拟环境(可以在 VS Code 左上角看到)并像这样在您的虚拟环境中安装包(例如 pandas):

conda install -n yourenvname [package]

0

你的pandas已经安装在Python3(3.7)中:

Requirement already satisfied: pandas in /Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages (0.24.0)

但是你正在运行Python2.7,而且Pandas不在你的2.7路径中:

['/Users/thibaultmonsel/Desktop/ML_folder', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', 
'/Library/Python/2.7/site-packages', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', 
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

尝试使用Python3简单运行您的脚本:

python3 help1.py

或者添加Python3头文件,例如:

#!/usr/bin/env python3

or 

#!/usr/local/bin/python3

如果那不起作用(就像我因为从jupyter笔记本的macos导入pandas而遇到了同样的问题),你最终可以从你的--user路径导入,例如:

sys.path.append("/Users/<USER>/Library/Python/3.7/lib/python/site-packages")

但请确保您已经安装了pandas(..python/site-packages/pandas),使用以下命令:

pip3 install pandas --user


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接