What I’m trying to do:
import a module that’s in the same folder (package). This syntax has worked (and is working) in other places in the app.
For example, it complains about “portfolios” (line 17) but not “participants” (line 16). (Line numbering in the error message seems to be off. The line number given is “16”, but the module name given is “portfolios”.)
As it happens, I have tables named portfolios
and participants
. But that should not create any problems for import
!
What I’ve tried and what’s not working:
Tried both in Chrome and in Edge. Same result in both cases.
Code Sample:
...
from . import participants # no complaint from IDE
from . import portfolios # loading error
...
Clone link:
Can send a link to Anvil support.
Do you have another module called portfolio
on another folder?
Anvil flattens the folders server_code
and client_code
, so having two modules with the same name on these two folders could cause conflicts.
I never had problems with table names conflicting with other modules.
Thanks, Stefano. I just checked. Exactly one module named portfolios
, in the position shown in the image. None named portfolio
. Though I was planning to add one…
If the folder trees are flattened, then I would expect there to be no difference between
from . import ...
and
from .. import ...
But the import
machinery seems to be very sensitive to these things. It doesn’t look to me like they’re being flattened, per se.
Anvil gave my project a __init__.py
containing
__path__ = [__path__[0]+"/server_code", __path__[0]+"/client_code"]
If Anvil’s server-side respects this setting, then if the module isn’t found in the tree rooted at server_code
, it will then search the tree rooted at client_code
. This could make it appear as if the two trees were merged.
Edit: Given the discrepancy between the given line# and the module name (line 16 tries to import participants
, not portfolios
), I do have a module named participant
(singular), several levels under client_code
.
Discrepancies with line numbers sometimes are caused by problems with dependencies… you think you are using a version, but the dependency is dipping into another one.
But in this case you are not mentioning any dependency, so…
Have you looked at circular imports?
Perhaps line 16 is indirectly trying to import portfolios
?
Thanks! It wasn’t circular, but you did get me to revisit a neighboring module, migrate_participant_format_from_none_to_1
, where the old-style import x
was being used instead of from . import x
. (Old habits, I guess.) I fixed those import
statements, and I’m off and running!
Of course, that means that the error message directed me not only to the wrong line, but the wrong module entirely…
1 Like