Simple import in python 3 errors with complaint about bytes

Mike Boyle moboyle79 at outlook.com
Tue Oct 21 02:20:54 CEST 2014


Ah, yes.  Thanks to that clever import debug hack, and the suggestion that it would actually be an import within the module causing the trouble, I managed to track it down.  I had tried copying some py3k changes from the "rational" module in numpy/numpy-dtypes, but evidently didn't do it right, or it just doesn't work.  I was using `PyImport_Import` with a PyString.  Now, going back to the original `PyImport_ImportModule` with a literal `char*`, everything seems to work great (for me at least; Travis isn't so sure...).

Thanks very much!




----------------------------------------
> From: ian.g.kelly at gmail.com
> Date: Mon, 20 Oct 2014 17:48:23 -0600
> Subject: Re: Simple import in python 3 errors with complaint about bytes
> To: python-list at python.org
>
> On Mon, Oct 20, 2014 at 5:29 PM, Mike Boyle <moboyle79 at outlook.com> wrote:
>> I'm modifying an extension written with the c-api to have a datatype of quaternions <https://github.com/moble/numpy_quaternion>, with one of the goals being python 3 support. It works nicely in python 2.7, but for python 3.x gives an error that I can't find anywhere on the google. The directory looks like this:
>>
>> .../site-packages/
>> quaternion/
>> __init__.py
>> numpy_quaternion.so
>>
>> __init__.py contains a line like this:
>>
>> from .numpy_quaternion import quaternion
>>
>> But when it hits that line, python 3 throws its hands up in disgust:
>>
>>> 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
>>
>> The only thing before that line is `import numpy as np`, which typically works just fine. Obviously, I'm using python3.4 to compile and (attempt to) import (with a conda environment), so it's not something as dumb as using the wrong python. Also, this is the result for both me on my laptop and Travis-CI <https://travis-ci.org/moble/numpy_quaternion>, so it doesn't seem to be anything peculiar to my installation. I've posted this question on stackoverflow, but haven't gotten much interest; the only responder suggested I ask here instead.
>>
>> Any ideas what's going wrong, or where I can go from here?
>
> I question whether the error is really coming from that particular
> import line. Try shadowing the __import__ function to see what's
> actually being passed there. E.g.:
>
>>>> orig_import = __import__
>>>> def debug_import(name, globals=None, locals=None, fromlist=(), level=0):
> ... print("debug_import:", name, globals, locals, fromlist, level)
> ... return orig_import(name, globals, locals, fromlist, level)
> ...
>>>> import builtins
>>>> builtins.__import__ = debug_import
>>>> import sys
> debug_import: sys {'__spec__': None, '__loader__': <class
> '_frozen_importlib.BuiltinImporter'>, '__package__': None, 'builtins':
> <module 'builtins' (built-in)>, 'orig_import': <built-in function
> __import__>, '__builtins__': <module 'builtins' (built-in)>,
> '__name__': '__main__', '__doc__': None, 'debug_import': <function
> debug_import at 0x7f6096aac9d8>} {'__spec__': None, '__loader__':
> <class '_frozen_importlib.BuiltinImporter'>, '__package__': None,
> 'builtins': <module 'builtins' (built-in)>, 'orig_import': <built-in
> function __import__>, '__builtins__': <module 'builtins' (built-in)>,
> '__name__': '__main__', '__doc__': None, 'debug_import': <function
> debug_import at 0x7f6096aac9d8>} None 0
> --
> https://mail.python.org/mailman/listinfo/python-list
 		 	   		  


More information about the Python-list mailing list