I am working in a Pycharm environment with the following organization:
main.py
foo/
- __init__.py
- bar.py
- egg.py
- foo.py
egg.py contains only one method:
def spam():
print("spam")
from bar.py, I can import egg.py, like so:
import egg
egg.spam()
and it works fine, except Pycharm underlines "egg" from import egg
as an error
No module named egg less... (Ctrl+F1) Inspection info: This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.
This "error" doesn't stop it from correctly executing egg.spam, though.
Is this a problem with Pycharm? If not, what would be the correct way to do that?
As a sidenote, if I try importing from foo instead of egg Pycharm does not notice any error, but I suspect it is mistaking the module foo.py with the package foo.
I also tried marking the main directory as "Sources Root" but it doesn't help: asking for
import foo.egg
in bar.py raises:
ModuleNotFoundError: No module named 'foo.egg'; 'foo' is not a package
Just to be extra clear I am not doing anything fancy here: I just select bar.py, right-click on it and select "run". I want this to work for any module without having to change settings every time I run a different module.