Search code examples
python-3.xpyramid

No module named error


I encountered an error, while i am practicing Login with authentication-pyramid framework - "init.py" as below,

File __init__.py, line 4, in <module>
from .security import groupfinder
ImportError: No module named 'myproject.security'

I have placed security.py file inside myproject folder. but when I changed import as,

from security import groupfinder

web app successfully runs.

My question is why .security throws error as "No module named 'myproject.security" while security.py is still inside myproject folder. Is that ".security" and "security" is different. What that operator "." corresponds to?


Solution

  • When importing files, Python uses the . notation to indicate submodules. In this case, since your two files are in the same folder (and since you are running inside the myproject namespace), there is no need to indicate a subdomain.

    If you had two modules, myproject and myproject2, and wanted to import a file from myproject2 into myproject, then you would need to use this notation.

    In all cases, however, importing .anything without being preceded with a module, such as myproject2.anything will cause an error. Hopefully this helps, I'm not great at explaining this stuff.