I have a project structure something like this...
/some_app
build/
README
out.py
some_app/
__init__.py
mod1.py
mod2.py
Now I want to import some_app
package into mod2
, without messing with sys.path
trickery. What I simply did is...
# mod2.py
import some_app
Now when I run the mod2.py from the command line
some_app $ python mod2.py
it throws error ImportError: No module named some_app
BUT, inside the out.py
file, when I do
# out.py
import some_app.mod2
and then do
some_app $ python out.py
it runs perfectly.
Hence, what is happening is this. I load a package in a module that is within the same package, and then run that module as the __main__
file -- and it doesn't work. Next, I load the same module (the one that I ran as __main__
) inside another module, and then run that another module as __main__
-- and it works.
Can someone please elaborate on what's going on here?
UPDATE
I understand that there is no straightforward reason for doing this -- because I could have directly imported any modules inside the some_app
package. The reason I am trying this is because, in the Django project, this is what they're doing. See this file for example
In every module, all the non-standard imports start with django.
. So I wondered why and how they are doing that.
UPDATE 2
Relevant links