Canedje
Posts: 263
Joined: Thu Mar 26, 2015 7:18 am

Conversion python 2 to 3.4

Fri Nov 04, 2016 8:39 pm

I do try to run a python2 programm with python 3 compiler.
I suddenly receive a lot of errors.
for example: st = time.time()
gives an error:

sudo python3 webserver.py

Code: Select all

 File "webserver.py", line 23
    ts = time.time()
                   ^
TabError: inconsistent use of tabs and spaces in indentation
I really don't understand what is wrong here.

Is there some tutorial showing the differents between Python 2 and 3?

User avatar
rpdom
Posts: 14686
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 8:44 pm

I think it is saying that you have some lines indented with space characters and some indented with tab characters and doesn't like the mix.

Canedje
Posts: 263
Joined: Thu Mar 26, 2015 7:18 am

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 8:47 pm

Thanks.
So it is not allowed to mix both in the syntax of the line?

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 9:03 pm

Canedje wrote:Thanks.
So it is not allowed to mix both in the syntax of the line?
Yes. I use an IDE where there's an option to convert all tabs to spaces, but this will work also:

Code: Select all

sed 's/\t/    /g' tab-file.py > no-tab-file.py
There are 10 types of people: those who understand binary and those who don't.

User avatar
Douglas6
Posts: 4710
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 9:09 pm

There's also a program, 2to3, that tries to do the conversion automatically: https://docs.python.org/2/library/2to3.html

Canedje
Posts: 263
Joined: Thu Mar 26, 2015 7:18 am

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 9:30 pm

Thanks this was the problem.

No I do have a new problem.
I'm using Tornado.
This was installed under Python2.7
Now under Python 3.4 it is not in the directory.
and generating the next error while compiling:

Code: Select all

Traceback (most recent call last):
  File "webservertst.py", line 2, in <module>
    import tornado.httpserver
ImportError: No module named 'tornado'
I tried to reinstall tornado by:
python -m pip install tornado

With no result
How to install Tornado for Python 3.4?

User avatar
kusti8
Posts: 3439
Joined: Sat Dec 21, 2013 5:29 pm
Location: USA

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 9:33 pm

Code: Select all

sudo pip3 install tornado
There are 10 types of people: those who understand binary and those who don't.

Canedje
Posts: 263
Joined: Thu Mar 26, 2015 7:18 am

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 9:57 pm

Thanks again.
Again one step further.
Now I do receive an import error for:

Code: Select all

   import MySQLdb as mdb
ImportError: No module named 'MySQLdb'
I quess i need to install Mysql for python 3.4?
I tried to reinstall mysql by using several methods without succes

User avatar
rpdom
Posts: 14686
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 10:04 pm

You probably need to install python3-mysqldb

Canedje
Posts: 263
Joined: Thu Mar 26, 2015 7:18 am

Re: Conversion python 2 to 3.4

Fri Nov 04, 2016 10:08 pm

I tried by using the next command:

sudo apt-get install python3-mysqldb

It said:

Code: Select all

Package python3-mysqldb is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

So this is not working

I also tried: sudo pip3 install pymysql
this gives:
Requirement already satisfied (use --upgrade to upgrade): pymysql in /usr/local/lib/python3.4/dist-packages
Cleaning up...

It suggest use upgrade

I did without result

I did find somewhere on the net:
Unfortunately MySQLdb does not support Python 3.
:cry:
So it looks like there is no solution

richrarobi
Posts: 271
Joined: Sun Feb 08, 2015 1:13 pm

Re: Conversion python 2 to 3.4

Sun Nov 06, 2016 10:24 am

This might help (from a quick Google) http://stackoverflow.com/questions/4960 ... -and-mysql

Stick at it, Python2 is fading (slowly, but it has an end of life plan, I believe) Python3 is better documented, etc, etc.
I decided to use python3 throughout, sometimes it is quite frustrating that code needs to be rebuilt.

I spent an evening converting an Adafruit library (stepper board control) then found someone had already done it (accidental search found it on github) !! Might stick with my own, though.....

Canedje
Posts: 263
Joined: Thu Mar 26, 2015 7:18 am

Re: Conversion python 2 to 3.4

Sun Nov 06, 2016 10:32 am

Thanks. This is a help

User avatar
DougieLawson
Posts: 35519
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Conversion python 2 to 3.4

Sun Nov 06, 2016 10:33 am

Canedje wrote:
I did find somewhere on the net:
Unfortunately MySQLdb does not support Python 3.
:cry:
So it looks like there is no solution
https://pypi.python.org/pypi/mysqlclient

You'll be OK on 12th April 2020 @ 12:00GMT with that.
Note: Having anything remotely humorous in your signature is completely banned on this forum. Wear a tinfoil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
rpdom
Posts: 14686
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Conversion python 2 to 3.4

Sun Nov 06, 2016 3:54 pm

Canedje wrote:I tried by using the next command:

sudo apt-get install python3-mysqldb

It said:

Code: Select all

Package python3-mysqldb is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

So this is not working

I also tried: sudo pip3 install pymysql
this gives:
Requirement already satisfied (use --upgrade to upgrade): pymysql in /usr/local/lib/python3.4/dist-packages
Cleaning up...

It suggest use upgrade

I did without result

I did find somewhere on the net:
Unfortunately MySQLdb does not support Python 3.
:cry:
So it looks like there is no solution
Ah, I seem to have the Raspbian Stretch repository enable on the Pi I was checking that package on. That's why you can't see it in Raspbian Jessie.

Canedje
Posts: 263
Joined: Thu Mar 26, 2015 7:18 am

Re: Conversion python 2 to 3.4

Sun Nov 06, 2016 3:56 pm

I indeeed now using pymysql
It is working fine

I changed my standard mysql import in:

Code: Select all

import pymysql as mdb
mdb.install_as_MySQLdb()
After that it is running like mysql

Return to “Python”