Question

I am running pypy and after adding the path to the bitarray library to sys.path I still can't import the module:

Python 2.7.3 (87aa9de10f9ca71da9ab4a3d53e0ba176b67d086, Feb 10 2014, 05:26:49)
[PyPy 2.2.1 with GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``every VM should come with neural
network trained to recognize microbenchmarks and randomly fluctuate them
+/-9000%''
>>>> import sys
>>>> sys.path.append('/usr/local/lib/python2.7/dist-packages/')
>>>> sys.path.append('/usr/local/lib/python2.7/dist-packages/bitarray/')
>>>> import bitarray
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/bitarray/__init__.py", line 11, in <module>
    from bitarray._bitarray import _bitarray, bitdiff, bits2bytes, _sysinfo
ImportError: No module named bitarray._bitarray

In regular python (CPython installed from aptitude) importing bitarray works fine and I haven't made any modifications to the module after installing it with pip. What's wrong here?

Just in case you need it here is the content of the bitarray dir:

$ls  /usr/local/lib/python2.7/dist-packages/bitarray/
_bitarray.so  __init__.py  __init__.pyc  test_bitarray.py  test_bitarray.pyc

UPDATE

After creating a virtualenv for pypy and installing bitarray with pip as Sunny suggested I still get an ImportError:

$ ls my-pypy-env/site-packages/bitarray
_bitarray.pypy-22.so  __init__.py  __init__.pyc  test_bitarray.py  test_bitarray.pyc


$ cd my-pypy-env/
~/my-pypy-env$ ./bin/activate 
~/my-pypy-env$ pypy
Python 2.7.3 (87aa9de10f9ca71da9ab4a3d53e0ba176b67d086, Feb 10 2014, 05:26:49)
[PyPy 2.2.1 with GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
And now for something completely different: ``__xxx__ and __rxxx__ vs operation
slots: particle quantum superposition kind of fun''
>>>> import sys
>>>> sys.path.append('/home/sofia/my-pypy-env/site-packages/bitarray')
>>>> import bitarray
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named bitarray

UPDATE 2

Sunny's solution is correct. The new problem was appending /site-packages/bitarray instead of just /site-packages/. I would have though that virtualenv would add this to the path automatically but it seems that it doesn't.

Was it helpful?

Solution

The _bitarray module looks like a CPython extension, which is written directly in C. CPython extension modules does not work directly in PyPy without any changes. You either need to install PyPy compatible version of the module, or do the required changes manually.

To install pypy compatible version, use the following commands:

# If pypy is installed globally
/path/to/pypy/pypy-2.1/bin/pip install bitarray

# If using virtualenv
source /path/to/virtualenv/env/bin/activate
pip install bitarray

Here are a couple of links from PyPy FAQs about this issue:

http://doc.pypy.org/en/latest/faq.html#module-xyz-does-not-work-with-pypy-importerror

http://doc.pypy.org/en/latest/faq.html#do-cpython-extension-modules-work-with-pypy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top