Import in python 3 complains about argument as str / bytes

I support updating the quaternion package to integrate with numpy so that it can be used in both python 2 and python 3. Unfortunately, the main import step failed miserably with 3.x, although it never lost with python 2.7. (I'm using python2.7 to compile 2.7 and python3.x to compile 3.x. It's a very simple distutils thing.) The error message doesn't even show up in google results and I just have no idea where to go from here.

Here's the complete output of a simple attempt to import a package:

> python -c 'import quaternion'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/Users/mynamehere/.continuum/anaconda/envs/py3k/lib/python3.4/site-packages/quaternion/__init__.py", line 3, in <module>
    from .numpy_quaternion import quaternion
TypeError: __import__() argument 1 must be str, not bytes

      

As the error message says, there is a line in __init__.py

saying

from .numpy_quaternion import quaternion

      

But why should this be problematic? There is a file numpy_quaternion.so

in the same directory as the file __init__.py

that appears to contain the appropriate characters. Travis-CI shows that it works fine in 2.7 (and the rest of the tests pass), but does not work in 3.2 and 3.4. So this is not something wrong with my python installation. I tried deleting .

for relative imports, but python couldn't find one numpy_quaternion

for imports (no surprise). I tried to change it to from quaternion.numpy_quaternion

, but I am getting the same error.

I see there have been changes to the import system in python 3 , but if anything, I would imagine it would be more py3k compatible than other ways to do it. Something went wrong? How can I get this to work?

To clarify, my hierarchy looks like this:

.../site-packages/
    quaternion/
        __init__.py
        numpy_quaternion.so

      

and the only thing that comes up in front of the problematic string is import numpy as np

, which is usually no problem.

+3


source to share


2 answers


People from the python list came back to me immediately with great suggestions. It turns out I imported something in numpy_quaternion.so

(using c-api), but the argument I was providing to this function was wrong. I (mostly) used code from a similar package :

PyObject* numpy_str = PyString_FromString("numpy");
PyObject* numpy = PyImport_Import(numpy_str);

      

I fixed it using



PyObject* numpy = PyImport_ImportModule("numpy");

      

And as Yu F. Sebastian points out in the comments, the reason I was embarrassed was because it PyString_FromString

was just #define

for the wrong function when I was using python 3.

+3


source


Since this is easy, I would try absolute imports first, although if my guesses below are correct it won't work.

from quaternion.numpy_quaternion import quaternion

      

From your post, I am assuming your hierarchy looks like



.../Libe/site-packages
    quaternion
    __init__.py
    numpy_quaternion.so
        quaternion  # a symbol in .so, not a .py

      

and that quaterion is a module and not a function or class. I'm guessing this because I can't imagine "numpy_quaternion" becoming bytes, and .so should return "quaternion" as bytes for 2.7 to work, so maybe it does the same with 3.x. My unix experience predates Python. But my impression is that 2.x and 3.x need separate .so. Or, if not, certain compilation flags may be required. If I'm right, you need to add 'numpy_quaternion_3x.so to your package and switch the import to sys.version [0].

If you don't get more answers here, try python-list, which is readily available at news.gmane.com as a news mirror gmane.comp.python.general. Regular respondents include power Linux users.

0


source







All Articles