Why does PyCharm underline these imports:
from oauth2_helper import (_url, get_token, get_session)
from config import scope
as Unresolved reference
, but if I add dots in front:
from .oauth2_helper import (_url, get_token, get_session)
from .config import scope
I don't get the error underline in PyCharm; then when running my app from the terminal, I get this error:
ModuleNotFoundError: No module named '__main__.oauth2_helper'; '__main__' is not a package
Why is this happening?
There are two things at play, here:
Like Eskapp says in the comment, PyCharm is configured to a different Project Root than you seem to expect, as evidenced by it marking your imports as unresolved references. You didn't provide any hints to your project structure, but its likely that your Project Root is set to the parent directory of your Python module (or even further up the hierarchy). PyCharm is looking for ${PROJECTROOT}/oath2_helper
and isn't finding it, because Project Root is set to the wrong directory.
You then try to fix this problem by changing to relative imports, which seems like a logical solution. It looks like you're directly calling the module in which those imports are written (i.e. python myapp.py
). When you call a module in this way, it drops information about the package structure, and no longer has any information about where other modules are relatively located. See https://stackoverflow.com/a/73149/11034626