Search code examples
pythonmoduleapache-tika

Python can't import tika


I am having trouble importing tika in a python file. I have spent a lot of time googling and been unable to find anything. Here is the iPython command: import tika, and subsequent stack trace.

It occurs to me that there could be a problem with a module on which tika is dependent, such as requests, or urllib3. However, when I try to install those with pip, it says requirement already satisfied. I have also double checked the PYTHONHOME director, and I'm 99% sure it's correct.

    $ ipython
Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)]
Type "copyright", "credits" or "license" for more information.

IPython 4.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.
WARNING: Readline services not available or not loaded.
WARNING: Proper color support under MS Windows requires the pyreadline library.
You can find it at:
http://ipython.org/pyreadline.html

Defaulting color scheme to 'NoColor'

In [1]: import tika
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
C:\cygwin64\lib\python3.6\site-packages\requests\packages\__init__.py in <module>()
     26 try:
---> 27     from . import urllib3
     28 except ImportError:

ImportError: cannot import name 'urllib3'

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
<ipython-input-1-9f3de0ba3e70> in <module>()
----> 1 import tika

C:\cygwin64\lib\python3.6\site-packages\tika\tika.py in <module>()
     18
     19 try:
---> 20     __import__('pkg_resources').declare_namespace(__name__)
     21 except ImportError:
     22     from pkgutil import extend_path

C:\cygwin64\lib\python3.6\site-packages\pkg_resources\__init__.py in declare_namespace(packageName)
   2161             # Ensure all the parent's path items are reflected in the child,
   2162             # if they apply
-> 2163             _handle_ns(packageName, path_item)
   2164
   2165     finally:

C:\cygwin64\lib\python3.6\site-packages\pkg_resources\__init__.py in _handle_ns(packageName, path_item)
   2096         path = module.__path__
   2097         path.append(subpath)
-> 2098         loader.load_module(packageName)
   2099         _rebuild_mod_path(path, packageName, module)
   2100     return subpath

C:\cygwin64\lib\python3.6\site-packages\tika\tika.py in <module>()
     89     open = codecs.open
     90
---> 91 import requests
     92 import socket
     93 import tempfile

C:\cygwin64\lib\python3.6\site-packages\requests\__init__.py in <module>()
     50 # Attempt to enable urllib3's SNI support, if possible
     51 try:
---> 52     from .packages.urllib3.contrib import pyopenssl
     53     pyopenssl.inject_into_urllib3()
     54 except ImportError:

C:\cygwin64\lib\python3.6\site-packages\requests\packages\__init__.py in <module>()
     27     from . import urllib3
     28 except ImportError:
---> 29     import urllib3
     30     sys.modules['%s.urllib3' % __name__] = urllib3
     31

C:\cygwin64\lib\python3.6\site-packages\urllib3\__init__.py in <module>()
      6 import warnings
      7
----> 8 from .connectionpool import (
      9     HTTPConnectionPool,
     10     HTTPSConnectionPool,

C:\cygwin64\lib\python3.6\site-packages\urllib3\connectionpool.py in <module>()
      9
     10
---> 11 from .exceptions import (
     12     ClosedPoolError,
     13     ProtocolError,

C:\cygwin64\lib\python3.6\site-packages\urllib3\exceptions.py in <module>()
      1 from __future__ import absolute_import
----> 2 from .packages.six.moves.http_client import (
      3     IncompleteRead as httplib_IncompleteRead
      4 )
      5 # Base Exceptions

ValueError: source code string cannot contain null bytes

Solution

  • In case anyone else is looking at this, here is how I finally solved my issue.

    I had been mistakenly assuming that the python-tika module was a fully packaged, ready to run version of tika. In fact, you need to download the java tika server from Apache, and it must be running when you use python-tika (you can easily just run the server on localhost).

    The Python-tika module then allows you to make requests to this server from your python code. I probably should have known this but for some reason I didn't pick it up in the documentation.