I've made the following workaround:

1. Put a try/except around the submodule.db = db statement.
2. Add a function to toplevel.py that can be called from the
controller to pass in a reference to db and assign it to a module
global.

Clunky, but it's ok for the moment.  Still interested to know if
there's a better way.


On Aug 7, 12:40 pm, Michael Ellis <[email protected]> wrote:
> I'm using the web2py shell mode to run a background process, let's call it
> toplevel.py,  that waits on pipe for incoming data, processes, and stores it
> in the db.  Works beautifully.  Even supports forking a child process that
> monitors the db and sends periodic reports and alerts by email to a system
> maintainer.
>
> I figured out that I can import submodules that access the db if toplevel.py
> does
>
> import submodule
> submodule.db = db
>
> Works fine in shell mode, but there are functions in toplevel.py that I'd
> like to test in a separate ordinary instance of web2py.  When I define, in a
> controller, a test function like this,
>
> def testDailyReport():
>        import toplevel
>        toplevel.db = db
>        toplevel.sendDailyReport()
>
> it fails on the import line because "db" isn't defined when it tries to set
> submodule.db.  My attempt to do this with "toplevel.db = db" obviously can't
> work because the NameError occurs before the import statement returns.
>
> What's the right way to import my toplevel script in a controller?  I'm
> using web2py 1.97 and my toplevel.py and submodule.py are in
> web2py/site-packages.
>
> Thanks,
> Mike

Reply via email to