pandas pandasql 不会导入:导入错误:无法导入名称 to_sql

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29755417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 23:13:47  来源:igfitidea点击:

pandasql will not import : ImportError: cannot import name to_sql

pythonmysqlsqlpandaspandasql

提问by cardamom

I installed pandasql with pip at the linux command prompt, and started ipython notebook:

我在linux命令提示符下用pip安装了pandasql,并启动了ipython notebook:

felix@xanadu ~ $ sudo pip install pandasql
[sudo] password for felix: 
Downloading/unpacking pandasql
  Downloading pandasql-0.6.2.tar.gz
  Running setup.py (path:/tmp/pip_build_root/pandasql/setup.py) egg_info for package pandasql

Installing collected packages: pandasql
  Running setup.py install for pandasql

Successfully installed pandasql
Cleaning up...
felix@xanadu ~ $ ipython notebook

Then tried to import pandas and it in the ipython notebook:

然后尝试在 ipython notebook 中导入 pandas 和它:

import pandas    
import pandasql

..and it's not happy, have looked around but there doesn't seem to be an answer anywhere. Here is the error message it gave:

..它不高兴,环顾四周,但似乎没有任何地方的答案。这是它给出的错误消息:

--------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-c9fa37159ca4> in <module>()
      1 import pandas
----> 2 import pandasql

/usr/local/lib/python2.7/dist-packages/pandasql/__init__.py in <module>()
----> 1 from .sqldf import sqldf
      2 import os
      3 import pandas as pd
      4 
      5 

/usr/local/lib/python2.7/dist-packages/pandasql/sqldf.py in <module>()
      2 import pandas as pd
      3 import numpy as np
----> 4 from pandas.io.sql import to_sql, read_sql
      5 import re
      6 import os

ImportError: cannot import name to_sql

Does anyone have any ideas? Cheers

有没有人有任何想法?干杯

回答by MattDMo

You should definitely upgrade to pandas0.16.0:

你绝对应该升级到pandas0.16.0:

sudo pip install -U pandas

I just looked through the source code of pandas/io/sql.pyin 0.13.1and 0.16.0, and the to_sql()and read_sql()methods are not present in the older version, while they exist in the latest version.

我刚刚通过的源代码看起来pandas/io/sql.py0.13.10.16.0,与to_sql()read_sql()方法不存在于旧版本,而他们在最新的版本存在。



To summarize our conversation in the comments, in order to successfully build pandas, you will need to install the gcc, g++, and python-devpackages from your system's package manager (apt-get, yum, zypper, whatever). If you are building for Python 3, the python3-devpackage is needed.

要总结我们的对话的意见,为了成功构建pandas,你将需要安装gccg++以及python-dev从系统的软件包管理器软件包(apt-getyumzypper,等等)。如果您正在为 Python 3 构建,python3-dev则需要该包。

If you are using Windows and the standard python.org version of Python, the easiest way to keep your packages up to date is to use Christoph Gohlke's Python Extension Packages for Windows repository. Many packages depend on his MKL-linked version of numpy, including pandas. The nice thing about all of these packages is that they are pre-compiled against both 32- and 64-bit versions of Python, and are generally available for Python 2.7, 3.3, and 3.4 (depending on the package, of course - some haven't been ported to Py3 yet). They are available in .whlformat, so installation/upgrading is as easy as

如果您使用的是 Windows 和 Python 的标准 python.org 版本,保持包最新的最简单方法是使用 Christoph Gohlke 的Python Extension Packages for Windows 存储库。许多软件包依赖于他的MKL 链接版本numpy,包括pandas. 所有这些包的好处是它们针对 32 位和 64 位版本的 Python 进行了预编译,并且通常可用于 Python 2.7、3.3 和 3.4(当然取决于包 - 有些避风港'尚未移植到 Py3)。它们以.whl格式提供,因此安装/升级就像

pip install -U name_of_package.whl