web2pyweb2py-modules

Web2py: ImportError: No module named patch while trying to import modules in the web2py Modules directory


I have a couple of custom modules present in the Modules directory of the web2py. These modules are easily imported to the controller file. But when I am trying to import these modules in the scheduler.py file I am getting the error. Kindly help.

/home/www-data/web2py$ python web2py.py -K <app_name>
web2py Web Framework
Created by Massimo Di Pierro, Copyright 2007-2018
Version 2.15.4-stable+timestamp.2017.09.02.04.02.22
Database drivers available: sqlite3, imaplib, pymysql, pg8000
starting scheduler for "<app_name>"...
Currently running 1 scheduler processes
Traceback (most recent call last):
  File "/home/www-data/web2py/gluon/restricted.py", line 219, in restricted
    exec(ccode, environment)
  File "applications/<app_name>/models/scheduler.py", line 28, in <module>
    customSSH = local_import('customSSH')
  File "/home/www-data/web2py/gluon/compileapp.py", line 444, in <lambda>
    local_import_aux(name, reload, app)
  File "/home/www-data/web2py/gluon/compileapp.py", line 347, in local_import_aux
    module = __import__(name)
  File "/home/www-data/web2py/gluon/custom_import.py", line 111, in custom_importer
    return NATIVE_IMPORTER(name, globals, locals, fromlist, level)
  File "applications/<app_name>/modules/customSSH.py", line 4, in <module>
    import multiprocessing
  File "/home/www-data/web2py/gluon/custom_import.py", line 111, in custom_importer
    return NATIVE_IMPORTER(name, globals, locals, fromlist, level)
  File "applications/<app_name>/modules/multiprocessing/__init__.py", line 64, in <module>
    import multiprocessing.patch
  File "/home/www-data/web2py/gluon/custom_import.py", line 111, in custom_importer
    return NATIVE_IMPORTER(name, globals, locals, fromlist, level)
ImportError: No module named patch

Solution

  • The problem is that multiprocessing is also the name of a module in the Python standard library. The initial import of multiprocessing is found in /modules by the web2py custom importer. However, when multiprocessing itself attempts to import multiprocessing.patch, the native Python importer expects the .patch to be a submodule of the standard library multiprocessing, which of course fails.

    If multiprocessing is a third-party library, it would probably be better to install it outside of the application's /modules folder. If it is a custom module or a module you are comfortable editing, you can either rename the module or change the imports to refer to the full applications.<app_name>.modules path -- for example:

    import applications.<app_name>.modules.multiprocessing.patch as patch