Search code examples
pythonmysqlsqlpandaspandasql

pandasql will not import : ImportError: cannot import name to_sql


I installed pandasql with pip at the linux command prompt, and started 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:

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


Solution

  • You should definitely upgrade to pandas 0.16.0:

    sudo pip install -U pandas
    

    I just looked through the source code of pandas/io/sql.py in 0.13.1 and 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.


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

    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 .whl format, so installation/upgrading is as easy as

    pip install -U name_of_package.whl