【问题标题】:How to import bitarray using pypy?如何使用 pypy 导入位数组?
【发布时间】:2014-03-07 10:56:17
【问题描述】:

我正在运行pypy,在将bitarray库的路径添加到sys.path后,我仍然无法导入模块:

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

在常规 python(从 aptitude 安装 CPython)中,导入 bitarray 工作正常,并且在使用 pip 安装模块后,我没有对模块进行任何修改。这里有什么问题?

以防万一你需要它是bitarray dir的内容:

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

更新

按照 Sunny 的建议,为 pypy 创建 virtualenv 并使用 pip 安装 bitarray 后,我仍然收到 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

更新 2

Sunny 的解决方案是正确的。新问题是附加/site-packages/bitarray 而不仅仅是/site-packages/。我会认为 virtualenv 会自动将其添加到路径中,但似乎没有。

【问题讨论】:

    标签: python importerror python-import pypy bitarray


    【解决方案1】:

    _bitarray 模块看起来像一个 CPython 扩展,它是直接用C 编写的。 CPython 扩展模块直接在 PyPy 中工作,无需任何更改。您要么需要安装 PyPy 兼容版本的模块,要么手动进行所需的更改。

    要安装 pypy 兼容版本,请使用以下命令:

    # 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
    

    以下是 PyPy 常见问题解答中关于此问题的几个链接:

    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

    【讨论】:

    • 有什么方法可以确保 bitarray 是用 C 编写的吗?
    • 不是整个位数组,而是 ImportError 中的子模块bitarray._bitarray。是的,它是:查看文件_bitarray.so.so 扩展名。
    • 请注意,如上面链接中所述,在 virtualenv 中运行 pip install bitarray 似乎工作正常。它成功编译了 C 代码,您无需使用 sys.path 破解。
    • 我发布了关于此的更新,执行此操作后我仍然收到 ImportError
    • 在启动 pypy 之前,您必须执行 source bin/activate 而不是 ./bin/activate Reference
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 2012-04-11
    相关资源
    最近更新 更多