klintkrossa
Posts: 61
Joined: Tue Nov 10, 2015 3:06 pm

imports 2 to 3

Postby klintkrossa » Thu Apr 27, 2017 10:15 pm

Hello,

Is there a way that python3.X.X able to import any python import either a python2 or python3 import?

For you guru's can it be made?
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.
scotty101
Posts: 2185
Joined: Fri Jun 08, 2012 6:03 pm

Re: imports 2 to 3

Postby scotty101 » Fri Apr 28, 2017 8:43 am

Python modules that are written for Python2 won't work with Python3 unless you rewrite them (or someone else has rewritten it). There is no magic command.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter
klintkrossa
Posts: 61
Joined: Tue Nov 10, 2015 3:06 pm

Re: imports 2 to 3

Postby klintkrossa » Fri Apr 28, 2017 4:03 pm

scotty101 wrote:Python modules that are written for Python2 won't work with Python3 unless you rewrite them (or someone else has rewritten it). There is no magic command.
I though so. Now there would be a python script.
Thanks
This is not like any other bulletin boards that I have been on. Been flamed on other BB's so bad I was afraid to ask.

All my Raspberry Pi's are like the Hessian artilleryman of Sleepy Hollow.
User avatar
kusti8
Posts: 3436
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: imports 2 to 3

Postby kusti8 » Fri Apr 28, 2017 6:32 pm

klintkrossa wrote:
scotty101 wrote:Python modules that are written for Python2 won't work with Python3 unless you rewrite them (or someone else has rewritten it). There is no magic command.
I though so. Now there would be a python script.
There is a script called 2to3 that can convert, but many simple scripts already have it done and complex ones are complicated to do.
There are 10 types of people: those who understand binary and those who don't.
User avatar
paddyg
Posts: 1884
Joined: Sat Jan 28, 2012 11:57 am

Re: imports 2 to 3

Postby paddyg » Fri Apr 28, 2017 10:56 pm

in pi3d we used the six module functions so the code gets converted 'on the fly' which means the same code runs with py2 or py3 (and pypy) However there is a small processing cost for doing this.

You *could* patch the 'wrong' path to dist packages using sys.path.insert(1,...) but some modules will almost certainly not work and it would end in a horrible mess.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d