Portable code: __import__ demands different string types between 2 and 3

Zachary Ware zachary.ware+pylist at gmail.com
Mon Dec 15 02:39:35 EST 2014


On Mon, Dec 15, 2014 at 1:29 AM, Ben Finney <ben+python at benfinney.id.au> wrote:
> How can I get that ‘__import__’ call, complete with its ‘fromlist’
> parameter, working correctly under both Python 2 and Python 3, keeping
> the ‘unicode_literals’ setting?

How about "str('bar')"?

> If some kind of kludge is needed to make it work between versions, is
> this a bug which should be fixed so “use Unicode for text” remains
> applicable advice?

Fixed in which version?  The problem is that Python 2 mixes the
notions of strings and bytestrings, and the solution is Python 3.

I believe six also has a "give me a native string" function to help with this.

-- 
Zach



More information about the Python-list mailing list