我似乎无法到进口熊猫包。我使用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) Thibaults-MBP-5d47:ML_folder thibaultmonsel$ 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/thibaultmonsel/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) Thibaults-MBP-5d47:ML_folder thibaultmonsel$ sudo pip3 install pandas --upgrade
Password:
The directory '/Users/thibaultmonsel/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/thibaultmonsel/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/thibaultmonsel/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/thibaultmonsel/Library/Python/3.7/lib/python/site-packages (from python-dateutil>=2.5.0->pandas) (1.12.0)
当我尝试使用后执行该程序我得到与上述相同的错误pip3 install pandas
...
如果可以帮助我,我也做了import.sys
:
base) Thibaults-MBP-5d47:ML_folder thibaultmonsel$ 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/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']
答案 0 :(得分:0)
您需要使用以下命令安装pandas
:
pip install pandas
如果遇到特权问题,则可能需要运行:
sudo pip install pandas
在Python 3上也可能需要运行:
pip3 install pandas
(虽然pip
可以被指向pip3
的话)。您可以在this SO post上了解点子版本之间的差异。
如果尚未安装pip
,请参见here进行安装。
答案 1 :(得分:0)
通过以下方法从环境中检查熊猫包路径:
jupyter kernelspec list
如果看到路径:
/Users/yourname/Library/Jupyter/kernels/yourenv
从Jupyter
删除Library
文件夹,然后再次运行。
答案 2 :(得分:0)
如果在您的IDE中看到such this,并且在运行代码时出现错误“没有名为pandas的模块”,则表明尽管已完成“ pip install pandas”或其他操作,但尚未安装pandas。
转到文件>设置>项目解释器,然后在软件包列表中查看熊猫是否可用。如果不是简单地单击+(加号),请选择pandas并将其安装在您的项目环境中。
see picture
然后等待您IDE更新您的项目框架...瞧,错误消失了!
答案 3 :(得分:0)
输入运行文件的命令时,请确保指定要使用的python版本。例如,使用filename.py
或python3 filename.py
python2 filename.py
答案 4 :(得分:0)
您的熊猫已安装在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,而熊猫不在您的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导入熊猫),则最终可以从--user路径导入,例如:
sys.path.append("/Users/<USER>/Library/Python/3.7/lib/python/site-packages")
,但是请确保使用
在其中安装了熊猫(..python / site-packages / pandas)。pip3安装熊猫--user
答案 5 :(得分:0)
检查您的虚拟环境(您可以在VS代码的左上角看到它),然后将软件包(例如pandas)安装在您的虚拟环境中,如下所示:
conda install -n yourenvname [package]
答案 6 :(得分:0)
在项目外安装pandas,我只想为env环境下载它,但我遇到了同样的错误,所以我从外面做了。
答案 7 :(得分:0)