I have read endless discussions on relative import in python, I think that one of the reasons it is so confusing is that it changes fro one Python version to another (my version is 3.6). But here the culprit seems to be PyCharm (unless i'm mistaken..) and i wonder if anyone came across a solution to this issue. for a project with this layout:
/project
|-- __init__.py
|---subfolder
|-- __init__.py
|-- AA.py
|-- BB.py
Let's imagine that AA.py contains some fuction myfunc
inside the file BB.py
if i write this import:
from AA import myfunc
Then python works perfectly, but PyCharm sees it as an error:
So to make PyCharm happy i can add the .
to the import and then the error is seemingly resolved:
from .AA import myfunc
But then python is not happy, giving me the error: ModuleNotFoundError: No module named '__main__.AA'; '__main__' is not a package
So to conclude, I use the import that actually works (i.e. from AA import myfunc
) but it would be great if i could make PyCharm agree to it somehow because then it offers features such as auto complete, go to definition and so on.
not duplicates: I know it seems like this subject was discussed over and over but it also has many aspects. Here i'm talking about the pycharm aspect and therefore this topic is new as far as i know.
- How does PyCharm handle relative imports of modules? - is a user who didn't add the root project dir to PYTHONPATH
- Pycharm auto relative imports - is talking about auto-import feature that is not the case here
- Subpackages and relative imports in PyCharm - is talking about import issue in python 2.7 but in here i'm not having any issue to import
- Relative imports for the billionth time - offeres a great review of importing issues and also features a very detailed answer - none of it helps in my case because i don't have any import issue. not to mention that it is python 2.7 topic and not 3.x