Python 2.7 to Python 3.8

I am trying to code a script in Python 3 and the example I am going off of is in 2.7 ( I was wondering since as far as I know Python 3 does not support the urllib2 import is there a simple conversion to Python 3?


1 Like


It may work to just change the import urllib2 to:

import urllib.request, urllib.error, urllib.parse

Or even better, your distro should have a Python 3 conversion script. You may be able to run something like this to convert the file from Python 2 to Python 3:

2to3-2.7 -w

(Edit: that said, the script appears to fail after conversion due to something else…)

My code now runs thanks @jebba

I do get this error now though

request = urllib.request("https://" + server_host + "/api/tokens")
    TypeError: 'module' object is not callable 

has (“https://” + server_host + “/api/tokens”) been changed?

1 Like

@Joe That looks like a semantic error in the code rather than a URL typo. I can’t provide help with bug hunting, but the URL looks correct.

TypeError: 'module' object is not callable 

The error is stating that you are trying to call urllib.request as though it were a function, but it is not a function, it is a module (and a module can’t be called). Not much of a Pythonista myself, but perhaps you need to call a function within the module?

1 Like