-
Notifications
You must be signed in to change notification settings - Fork 234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix wrong import logic when loading modules #140
Conversation
Only load module within package, not all accessible modules Loading all accessible modules causes a bug when an accessible module has a same name with module inside package
1aa109d
to
13b1d69
Compare
@blackthunder0812 Thanks for your contribution! 🎉 Could you please elaborate a bit on the bug so that I can reproduce it? |
@wochinge For example, if we have a project structure like this:
Then action server cannot load the module inside
Reproducing bug with above project structure:
Because we have 2 directories with the same name
Then we can fix by loading only modules inside packages, recursively, don't load all accessible modules |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried with this structure
.
└── test
├── __init__.py
├── actions.py . # imports module1 and module2
├── other
│ ├── __init__.py
│ └── utils
│ ├── __init__.py
│ └── module1.py
└── utils
├── __init__.py
└── module2.py
and running rasa run actions --actions test
which works fine. When I use your fix it actually breaks (gettig ModuleNotFoundError: No module named 'test.testactions'
) .
Which command are you using to run the action server and did you include all the __init__
files to mark the directories as Python packages?
Thanks for pointing out the problem. My project structure is different, please look at images below, (actions folder and utils folder are at same level) this is before patch: This is after new patch: |
ff8200f
to
587ab88
Compare
587ab88
to
47603fe
Compare
@wochinge Can you please try again? |
@blackthunder0812 I tried again, and your change works, but
I adapted my structure to yours: .
├── __init__.py
├── actions.py
├── other
│ ├── __init__.py
│ └── utils
│ ├── __init__.py
│ └── module1.py
└── utils
├── __init__.py
└── module2.py And then run from utils.module2 import MODULE_2
from other.utils.module1 import MODULE_1 Works like a charm |
The problem does not lie on importing module in
Then run:
with Before patching:
Please look at screenshot below The difference between ours is that my |
In my opinion the error in your current directory structure makes sense. You are importing the |
But I don't actually import anything, please note that all files are empty. Then the problem is that even if I don't import any module at outer The project structure as above is quite popular and should be valid I think. |
Even if the files are empty, you're still importing a module (even if it's empty), no?
Can you maybe upload zip for me so that I can reproduce it? I think that would make it easier 🙏 |
@wochinge Please check, test command:
|
Sorry, for the super long delay @blackthunder0812 . Finally I could re-produce the problem 👍 🙏 Two things:
for loader, name, is_pkg in pkgutil.walk_packages(package.__path__):
if loader.path not in package.__path__:
continue
# rest of the code
Thanks again for being so patient! |
|
Only load module within package, not all accessible modules
Loading all accessible modules causes a bug when an accessible module has a same name with module inside package
Proposed changes:
Status (please check what you already did):
black
(please check Readme for instructions)