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

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

我在 web2py 的模块目录中有几个自定义模块。这些模块很容易导入到控制器文件中。但是当我试图在 scheduler.py 文件中导入这些模块时,我收到了错误。请帮忙。

/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

问题是 multiprocessing 也是 Python 标准库中模块的名称。 multiprocessing 的初始导入由 web2py 自定义导入程序在 /modules 中找到。但是,当 multiprocessing 本身尝试导入 multiprocessing.patch 时,本地 Python 导入器期望 .patch 是标准库 multiprocessing 的子模块,这当然失败。

如果 multiprocessing 是第三方库,最好将其安装在应用程序的 /modules 文件夹之外。如果它是自定义模块或您可以轻松编辑的模块,您可以重命名模块或更改导入以引用完整的 applications.<app_name>.modules 路径——例如:

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