Getting Error ModuleNotFoundError: No module named 'urllib2'

1,454 views
Skip to first unread message

Suhas Jadhav

unread,
Jun 4, 2018, 9:44:37 AM6/4/18
to web2py-users
Hi,
I am getting error ModuleNotFoundError: No module named 'urllib2'

My Code is 

def test():
    import ldap
    from urllib.request import urlopen
    import urllib2
    import urllib
    auth.settings.actions_disabled=['register','change_password','request_reset_password','retrieve_username','profile']
    auth.settings.remember_me_form = False
    from gluon.contrib.login_methods.ldap_auth import ldap_auth
    auth.settings.login_methods = [ldap_auth(mode='ad',
    server='sever.com',
    base_dn='dc="DC,dc=corp,dc="DC",dc=com')]
    
    from gluon.contrib.login_methods.basic_auth import basic_auth
    auth.settings.login_methods.append(
    basic_auth('https://example.com'))
    return locals()


ERROR

Traceback (most recent call last):
File "C:\WEB2PY\web2py\gluon\custom_import.py", line 77, in custom_importer
return NATIVE_IMPORTER(oname, globals, locals, fromlist, level)
ModuleNotFoundError: No module named 'urllib2'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\WEB2PY\web2py\gluon\custom_import.py", line 90, in custom_importer
result = result or sys.modules[modules_prefix+'.'+itemname]
KeyError: 'applications.welcome.modules.urllib2'

All the imports work from python terminal but not from web2py
import ldap
    from urllib.request import urlopen
    import urllib2
    import urllib


VERSION
web2py™ Version 2.16.1-stable+timestamp.2017.11.14.05.54.25

PYTHON Python 3.6.2


Any solution or workaround?

黄祥

unread,
Jun 4, 2018, 10:39:13 AM6/4/18
to web2py-users

Marcelo Huerta

unread,
Jun 4, 2018, 10:40:56 AM6/4/18
to web2py-users

El lunes, 4 de junio de 2018, 10:44:37 (UTC-3), Suhas Jadhav escribió:

web2py™ Version 2.16.1-stable+timestamp.2017.11.14.05.54.25

PYTHON Python 3.6.2


Any solution or workaround?

Using proper Python3 code?
Module urllib2 does not exist in Python 3.

Suhas Jadhav

unread,
Jun 4, 2018, 1:54:01 PM6/4/18
to web2py-users
Yes, I say the documentation. But issues here is urllib2 works well from my python console.
the error happens when web2py tries to call it.
My code is not calling it directly. I believe ldap_auth library calling urllib2 internally and faile.

Thanks,
Suhas

Marcelo Huerta

unread,
Jun 4, 2018, 3:09:35 PM6/4/18
to web...@googlegroups.com
2018-06-04 14:54 GMT-03:00 Suhas Jadhav <jadhav...@gmail.com>:
>
> Yes, I say the documentation. But issues here is urllib2 works well from my python console.

The Python3 console?
Unless you have done something weird, urllib2 cannot work in the
Python3 console *because the module does not exist*.

The proper course of action is to migrate the code correctly to Python
3. You may have to extract the code and run it through 2to3, or a
similar process, but be as it may, *code legitimally intended to run
in Python3 shouldn't invoke a library which is not supposed to exist
there*.

This is not a web2py problem.

Dave S

unread,
Jun 4, 2018, 8:25:09 PM6/4/18
to web2py-users


On Monday, June 4, 2018 at 10:54:01 AM UTC-7, Suhas Jadhav wrote:
Yes, I say the documentation. But issues here is urllib2 works well from my python console.
the error happens when web2py tries to call it.
My code is not calling it directly. I believe ldap_auth library calling urllib2 internally and faile.

Thanks,
Suhas

Your sample in the original post showed YOU importing urllib2.  My scan of gluon/contrib/login_methods/ldap_auth.py doesn't find an import of urllib2.

/dps
 

Suhas Jadhav

unread,
Jun 8, 2018, 1:07:20 AM6/8/18
to web2py-users
Even if I use below code in the controller. Just the import
I am getting error 
Traceback (most recent call last):
File "C:\WEB2PY\web2py\gluon\custom_import.py", line 77, in custom_importer
return NATIVE_IMPORTER(oname, globals, locals, fromlist, level)
ModuleNotFoundError: No module named 'urllib2' 

*************************************************************************
def test():
    from gluon.contrib.login_methods.basic_auth import basic_auth
       
    return locals()

*************************************************************************

On Monday, 4 June 2018 06:44:37 UTC-7, Suhas Jadhav wrote:

Leonel Câmara

unread,
Jun 8, 2018, 5:43:19 AM6/8/18
to web2py-users
Suhas can you try these changes:




Reply all
Reply to author
Forward
0 new messages