【问题标题】:import pandas as pd ImportError: No module named pandasimport pandas as pd ImportError: No module named pandas
【发布时间】:2019-06-27 01:40:08
【问题描述】:

我似乎无法导入熊猫包。我使用 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.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 了吗?
  • 请清理您的输出以仅显示与您的问题相关的部分。您很可能没有安装 pandas。我猜您可能正在使用一个名为“base”的虚拟环境?您需要在该环境内安装所有必要的软件包。

标签: python-3.x pandas import importerror


【解决方案1】:

你需要安装pandas:

pip install pandas

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

sudo pip install pandas

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

pip3 install pandas(尽管pip 可能已经指向pip3)。您可以在 this SO post 上了解 pip 版本之间的差异。

如果您没有安装pip,请参阅here 进行安装。

【讨论】:

  • 你好 PJW,我修改了我的帖子。我尝试了你所有的建议,但似乎没有用。我在网上冲浪仍然没有答案....
  • 是的,看起来 Pandas 包的安装路径与您的 Python 安装路径不同。如果您在 base 虚拟环境中做所有事情,那么它应该可以工作.....不确定。
【解决方案2】:

检查您的环境中的 pandas 包路径:
jupyter kernelspec list

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

从Library 中删除该Jupyter 文件夹并再次运行。

【讨论】:

    【解决方案3】:

    如果您在 IDE 中看到 such this 并且在运行代码时出现错误“没有名为 pandas 的模块”,这意味着虽然您已经完成了“pip install pandas”或其他操作,但尚未安装 pandas。

    转到文件>设置>项目解释器并查看包列表中是否有pandas。如果不是简单地单击 +(加号),请选择 pandas 并将其安装在您的项目环境中。
    see picture 然后等你 IDE 更新你的项目骨架...瞧,错误消失了!

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        您的 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 header,例如:

        #!/usr/bin/env python3
        
        or 
        
        #!/usr/local/bin/python3
        

        如果这不起作用(就像我遇到了同样的问题,因为我是从 jupyter notebook、macos 导入熊猫),你最终可以从你的 --user 路径导入,例如:

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

        但请确保您使用

        在那里安装了 pandas (..python/site-packages/pandas)

        pip3 安装熊猫 --user

        【讨论】:

          【解决方案6】:

          检查您的虚拟环境(您可以在 VS 代码的左角看到它)并在您的虚拟环境中安装包(例如 pandas),如下所示:

          conda install -n yourenvname [package]
          

          【讨论】:

            【解决方案7】:

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

            【讨论】:

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2017-12-08
                • 2013-09-14
                • 1970-01-01
                • 2017-02-20
                • 2018-01-03
                • 2017-11-02
                • 2017-10-04
                相关资源
                最近更新 更多