我在web2py的modules目录中有几个自定义模块。这些模块很容易导入控制器文件。但是,当我试图在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