3

Whenever I try to import pandas, whether inside a virtualenv or otherwise I am always getting this error.

Python 3.6.2 |Anaconda custom (64-bit)| (default, Sep 19 2017, 08:03:39) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
Traceback (most recent call last):
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 26, in <module>
from pandas._libs import (hashtable as _hashtable,
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\_libs\__init__.py", line 3, in <module>
from .tslib import iNaT, NaT, Timestamp, Timedelta, OutOfBoundsDatetime
ModuleNotFoundError: No module named 'pandas._libs.tslib'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 35, in <module>
"the C extensions first.".format(module))
ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.

I tried the follwoing solutions:

  1. Cloning pandas from git and running SETUP.py (on an instance of python 3.6 installed directly into my win10 os)
  2. Using anaconda as python distribution and conda to install pandas
  3. Updating microsoft visual c++ 2017 redistributable
  4. Updating C:\ProgramData\Anaconda3\Lib\site-packages\PyInstaller\hooks\hook-pandas.py

None of these seem to work. Please help me understand what the issue here is.

OriolAbril
  • 7,315
  • 4
  • 29
  • 40
ISHAN BOSE
  • 31
  • 1
  • 1
  • 2
  • Wich flags did you set in option 1 while running setup.py? – OriolAbril Apr 29 '18 at 18:24
  • What is `AppData\Roaming`? It says "Anaconda custom" in the prompt, how did you install anaconda? – Andy Hayden Apr 29 '18 at 18:43
  • @xg.plt.py: I used [python setup.py build_ext --inplace --force] – ISHAN BOSE Apr 30 '18 at 01:44
  • @AndyHayden: Downloaded Anaconda3 installer and followed the normal installation process – ISHAN BOSE Apr 30 '18 at 01:47
  • @ISHANBOSE you shouldn't have to do the `setup.py build_ext` that's only if you are building from source, which you shouldn't be. My guess is the virtual env is not using anaconda, and the install of pandas is messed up (perhaps created before installing anaconda?). I would delete this directory `C:\Users\ishan\AppData\Roaming\Python\Python36` and see if that helps. – Andy Hayden Apr 30 '18 at 11:57
  • @AndyHayden... It worked ... thanks a lot. How did you figure out that it was the issue? – ISHAN BOSE Apr 30 '18 at 16:11
  • @ISHANBOSE that exception suggests the pandas it was looking for wasn't correctly built, anaconda is usually pretty good at building pandas and putting it in the right place! – Andy Hayden Apr 30 '18 at 16:25

5 Answers5

2

This exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\ishan\AppData\Roaming\Python\Python36\site-packages\pandas\__init__.py", line 35, in <module>
"the C extensions first.".format(module))
ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first

suggests that pandas was not built properly during installation.

The latter sentence:

If you want to import pandas from the source directory, you may need to run python setup.py build_ext --inplace --force to build the C extensions first

Is really only something you ought to be doing if you are contributing to pandas source code (e.g. to fix a pandas bug or add a feature to pandas itself) to the pandas-dev github repository*.
Most likely you shouldn't be building from source in your project.

Generally anaconda is pretty good at installing pandas correctly, and so my guess/comment was:

My guess is the virtual env is not using anaconda, and the install of pandas is messed up (perhaps created before installing anaconda?). I would delete this directory C:\Users\ishan\AppData\Roaming\Python\Python36 and see if that helps

The reason I suggested that directory was because it was in the error message AND it doesn't look like somewhere I expect anaconda's installation of pandas to be (either generally or as a virtualenv).


* Note: this is something fun to do, to give back to the pandas community: there's some low-hanging fruit, typos or code changes, so I recommend investigating whether there's any way you can contribute.

Andy Hayden
  • 359,921
  • 101
  • 625
  • 535
2

If you are using a Conda distribution (e.g., AnaConda, MiniConda), as it seems to be the case, uninstalling and reinstalling Pandas may help.

Run the following commands on the cmd console:

conda uninstall pandas

conda install pandas
0

I run into same error when setting up python, keras and anything between. Background: I installed anaconda and followed instructions by https://www.youtube.com/watch?v=z0qhKP2liHs and instruction to downgrade to python 3.6 by http://docs.anaconda.com/anaconda/user-guide/faq/#how-do-i-get-the-latest-anaconda-with-python-3-5

Running from Jupyter I run into same problem as author I was able to solve my problem by: - uploading pandas version for python 3.6 per https://docs.anaconda.com/anaconda/packages/py3.6_win-64/

  • then I run python from command line .. it worked

  • then I tested with PyCharm .. it worked

Appears that either Anaconda&Jupyter combination did not work or selecting pandas version did the job.

0

since tslib has been deprecated for the latest version of pandas. try to remove the pd.tslib.Dataframe and replace with pd.DataFrame where ever tslib is present in ggplot library. it works !!

you can find the packages in the lib file of ggplot folder.

Thank you!

0

Not sure if this is anything like an exhaustive answer, but seems related.

I came by this question as I was building Python from source (were you too perhaps?) and using that then to build an installer for my app - at some point I managed to get the exception from the OP:

ImportError: C extension: No module named 'pandas._libs.tslib' not built. If you want to import pandas from the source directory, you may need to run 'python setup.py build_ext --inplace --force' to build the C extensions first.

What I then did was trying to import the module in question from a newly opened Python REPL (i.e. import pandas._libs.tslib). It turned out to be a useful effort as what I there got was an error likely about a missing _bz2.

By then I had already been through installing a good number of Linux packages that needed to be present prior to running the Python's ./configure so that they get included (for pip to operate well, for instance), and it seemed obvious that I just missed yet another package.

Indeed, as found to be suggested here in response to that error, a simple

sudo apt-get install libbz2-dev

and re-running ./configure, make, make install put me into a situation when finally pandas was willing to be loaded.

However, now it complains for the missing lzma extension :) such is life:

UserWarning: Could not import the lzma module. Your installed Python is incomplete. Attempting to use lzma compression will result in a RuntimeError.

From my perspective this finally seems like a soft error (app starts yay!)

My speculative explanation is that due to the lack of bzip2 available for Python at the time building and then this being missing, Pandas gives a slightly misleading error and fails to load, whether or not it correctly assesses that to be a show-stopper error. Possibly it's such a rare situation that from a time when it was commonplace/intuitive to fix, they may have stopped maintaining it and now it isn't telly enough?


Update: in case anyone wondered, the lzma dependency warning can be alleviated via sudo apt-get install liblzma-dev see UserWarning: Could not import the lzma module. Your installed Python is incomplete and rebuilding in my case Python and then the PyInstaller packaged app, by the way somewhere along the sequence this includes reinstalling Pandas.

brezniczky
  • 492
  • 3
  • 10