python-3.x 导入Pandas作为pd ImportError:没有名为panda的模块

yhqotfr8  于 2023-02-10  发布在  Python
关注(0)|答案(9)|浏览(227)

我似乎不能导入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.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']
iugsix8n

iugsix8n1#

您需要安装pandas和:
pip install pandas
如果遇到权限问题,可能需要运行:
sudo pip install pandas
在Python 3上也可能需要运行:
pip3 install pandas(尽管pip可能已经指向pip3).您可以在this SO post上阅读pip版本之间的差异。
如果您没有安装pip,请参阅here进行安装。

yws3nbqq

yws3nbqq2#

使用以下命令从您的环境中检查panda包路径:
jupyter kernelspec list
如果看到路径:/Users/yourname/Library/Jupyter/kernels/yourenv
Library中删除该Jupyter文件夹,然后再次运行。

gzjq41n4

gzjq41n43#

如果您在IDE中看到such this,并且在运行代码时看到错误“no module named panda”,则意味着尽管您已经执行了“pip install panda”或其他操作,但尚未安装panda。
转到文件〉设置〉项目解释器,看看列表中是否有panda。如果没有,只需点击+(加号),选择panda并将其安装到您的项目环境中。
see picture然后等待您的IDE更新您的项目框架...瞧,错误消失了!

f45qwnt8

f45qwnt84#

输入命令运行文件时,请确保指定所使用的python版本。例如,使用python3 filename.pypython2 filename.py代替python filename.py

cgh8pdjw

cgh8pdjw5#

你的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 notebook,macos导入Pandas时遇到了同样的问题),你可以最终从你的--用户路径导入,例如:

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

但是要确保你在那里安装了panda(.. python/site-packages/panda)
pip 3安装Pandas--用户

pftdvrlh

pftdvrlh6#

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

conda install -n yourenvname [package]
jecbmhm3

jecbmhm37#

安装Pandas以外的项目,我想下载它只为一个env环境,但我得到了同样的错误,所以我这样做,从外面。

gxwragnw

gxwragnw8#

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

h7appiyu9#

对于我来说,下面的命令在MAC中有效
sudo -H pip 3安装Pandas--升级

相关问题