Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Pypy to recognize third party modules

Just a quick question, how do I get pypy to recognize third pary modules that I have in Python? For instance, I get the following error.

from tables import *
ImportError: No Module named tables

Which is basically saying that it cannot find my pytables library that I use to interact with in the script I am trying to run.

like image 695
jab Avatar asked Jun 25 '12 18:06

jab


People also ask

Does PyPy support all libraries?

Compatibility: PyPy is highly compatible with existing python code. It supports cffi, cppyy, and can run popular python libraries like twisted, and django. It can also run NumPy, Scikit-learn and more via a c-extension compatibility layer.

Which is the correct Sytax for installing third party module?

The command to install the module is “python setup.py install”. Type that into the prompt so that it resembles the following and hit enter. Setuptools should now be installed. Run the command “easy_install --version” to make sure.

How do I install a third party module in Python?

The primary way to install third-party modules is to use Python's pip tool. This tool securely downloads and installs Python modules onto your computer from https://pypi.python.org/, the website of the Python Software Foundation. PyPI, or the Python Package Index, is a sort of free app store for Python modules.

Can I use PIP with PyPy?

Pip/pip3 is the official package manager for Python, but it can also be used by PyPy for installing Python modules.


2 Answers

For pure python modules, just add the directory containing the modules to your sys.path, using something like:

sys.path.insert(0, '/usr/local/lib')
sys.path.insert(0, os.path.expanduser('~/lib'))

This works for CPython, Pypy and Jython.

For C extension modules, you can try Pypy's cpyext, but it won't run everything you might hope for, because some CPython C extension modules wander into dark corners of CPython's C-based runtime: http://morepypy.blogspot.com/2010/04/using-cpython-extension-modules-with.html

I sometimes write code that uses ctypes to interface with a C .so, and then use that on both CPython and Pypy, because they both do pretty well with ctypes - but ctypes can be kinda slow on CPython: http://docs.python.org/library/ctypes.html Last I checked, Jython had the beginnings of ctypes, but it wasn't far enough along to use, at least not for my purposes.

There's also a new interface that requires a C compiler at runtime. It'll likely be less brittle (read: prone to segfaults) than ctypes. It's described here: http://morepypy.blogspot.com/2012/06/release-01-of-cffi.html It comes from the Pypy project I believe, but it was made to work first on CPython. AFAIK, it doesn't yet run on Pypy.

like image 59
user1277476 Avatar answered Sep 20 '22 06:09

user1277476


Pypy has a separate install space. Therefore, any modules you want to install from pypi should be installed into its space. So, for instance, I have pypy installed in /usr/local/pypy-1.9-32bit

I recommend using pip or easy_install. Here's how to install pip:

curl curl https://bootstrap.pypa.io/get-pip.py | /usr/local/pypy-1.9-32bit/bin/pypy

or

curl https://raw.github.com/pypa/pip/master/contrib/get-pip.py | /usr/local/pypy-1.9-32bit/bin/pypy

Then, just use the newly installed pip to get the module:

sudo /usr/local/pypy-1.9-32bit/bin/pip install tables

In this case, it failed, with the following error:

bminton@bminton:/tmp$ sudo /usr/local/pypy-1.9-32bit/bin/pip install tables
Downloading/unpacking tables
  Downloading tables-2.4.0.tar.gz (8.9Mb): 8.9Mb downloaded
  Running setup.py egg_info for package tables
    .. ERROR:: You need numpy 1.4.1 or greater to run PyTables!
    Complete output from command python setup.py egg_info:
    .. ERROR:: You need numpy 1.4.1 or greater to run PyTables!

Installation failed in this case, because Tables depends on Numpy, which is not yet supported by PyPy (although they are working on it, see http://pypy.org/numpydonate.html). However, for many python modules, this method works great. For instance, I successfully installed the logilab constraint package this way.

like image 45
Brian Minton Avatar answered Sep 18 '22 06:09

Brian Minton