首页 > 解决方案 > import pandas as pd ImportError: No module named pandas

问题描述

我似乎无法导入熊猫包。我使用 Visual Studio 代码进行编码。我使用 Mac 并拥有 osX 10.14 Majove。

我试图编译的代码是:

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.sysif can help :

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']

标签: python-3.xpandasimportimporterror

解决方案


您需要安装pandas

pip install pandas

如果您遇到权限问题,您可能需要运行:

sudo pip install pandas

您可能需要在 Python 3 上运行:

pip3 install pandas(虽然pip可能已经指向pip3)。您可以在此 SO 帖子中了解 pip 版本之间的差异。

如果您还没有pip安装,请参阅此处进行安装。


推荐阅读