Modules Import Problem.

84 views
Skip to first unread message

Scott ODonnell

unread,
Jul 20, 2018, 7:55:20 AM7/20/18
to web2py-users
I have an API Wrapper library I'm trying to use in my Web2PY project by including it in the modules directory.
I'm copying a directory called myWrapper into the modules directory.

The result is applications/myApp/modules/myWrapper
Within the myWrapper directory are all the files that make up the myWrapper library 

The problem is that I need to load a class from a file within the MyWrapper Library.
In any vanilla python script I would simply do the following

From myWrapper.fileA import classA

I'm not sure how/if I can do the same thing inside Web2PY using import_local().

Further, I did attempt to modify the python sys.path to include the myWrapper directory , which allowed me to use:
From myWrapper.fileA import classA 

BUT .... Within classA there are additional imports (from other files within the myWrapper directory) that fail to import.
I get  the "No module named .... " error.

Is there a proper way to get all this working?

Thanks in advance
Scott



Anthony

unread,
Jul 20, 2018, 11:49:26 AM7/20/18
to web2py-users
local_import was deprecated several years ago (I don't think it's even mentioned in the book any more). Just use regular imports. There shouldn't be any need to change sys.path. If you still have problems, we may need to see some real code and names. There could be issues if your module has the same name as another in system (this isn't a problem at the top level of /modules, but can cause problems when there are submodules).

Anthony

Dave S

unread,
Jul 20, 2018, 5:39:56 PM7/20/18
to web2py-users


On Friday, July 20, 2018 at 8:49:26 AM UTC-7, Anthony wrote:
local_import was deprecated several years ago (I don't think it's even mentioned in the book any more). Just use regular imports. There shouldn't be any need to change sys.path. If you still have problems, we may need to see some real code and names. There could be issues if your module has the same name as another in system (this isn't a problem at the top level of /modules, but can cause problems when there are submodules).

Anthony


Is __init__.py required in all the new directories?

/dps
 

Anthony

unread,
Jul 20, 2018, 6:18:52 PM7/20/18
to web2py-users
On Friday, July 20, 2018 at 5:39:56 PM UTC-4, Dave S wrote:


On Friday, July 20, 2018 at 8:49:26 AM UTC-7, Anthony wrote:
local_import was deprecated several years ago (I don't think it's even mentioned in the book any more). Just use regular imports. There shouldn't be any need to change sys.path. If you still have problems, we may need to see some real code and names. There could be issues if your module has the same name as another in system (this isn't a problem at the top level of /modules, but can cause problems when there are submodules).

Anthony


Is __init__.py required in all the new directories?

Yes.

Alfonso Serra

unread,
Jan 2, 2019, 4:25:48 AM1/2/19
to web2py-users
Hi, im having a similar issue:

In modules, i have a utils.py module with a function called "first" that i cant import from another module.

The problem is that theres a utils.py module installed in the python standard library that obviously doesn not has that function and its being imported before my utils.py.

So the questions are:
- shall i modify sys.path so modules takes precedence over the python library?
- is there any drawback you can think of by doing so?
- where would it be a good place to do this?

I could rename the module to myutils.py and the problem goes away but still would like to load web2py modules first.

Thanks



Alfonso Serra

unread,
Jan 2, 2019, 5:49:56 AM1/2/19
to web2py-users
Hi, so far it looks like this works:

at the model 01_config.py

import sys
sys
.path.insert(0, r"E:\webdev\web2py-mod.02\applications\myapp\modules")

then im able to import as:
from utils import myfunc

where utils.py is an application module at applications\myapp\modules

Massimo Di Pierro

unread,
Jan 5, 2019, 12:59:41 PM1/5/19
to web2py-users
Do not do this. The models are executed at every http call and you end up with a longer and longer sys.path (memory leak).
The problem is with the module itself, it does not uses relative imports.
I recommend you put the module in web2py/site-packages

Alfonso Serra

unread,
Jan 6, 2019, 7:43:49 PM1/6/19
to web2py-users
Hi Massimo, thanks for your answer.

sites-packages is an option indeed.

Importing modules works without issues when i fully qualify the import path like:

from applications.myapp.modules.utils import anyfunc

I have been reading the custom_import, which tries to find or reload the module unsuccesfully if its written shorthanded. (from utils import anyfunc)
Theres also an example at compileapp.py on how to use local_import like d = local_import('a.b.c')

Ill try to move the sys.path.insert somewhere to be executed just once for now.
Thanks.
Reply all
Reply to author
Forward
0 new messages