0

structure:

-- tst
------ __init__.py
------ app.py

assume execution is as follows:

python27 app.py

assume __init__.py has another import in it, for the sake of question, import os

if i execute app.py directly, how do I ensure __init__.py is executed also? assume there are MANY modules called tst, and calling import tst from app.py is ambiguous (or is it?).

is there something like this:

app.py:
import . # import parent folder module, basically tell py27 to execute __init__.py

yes, i understand i can solve all my issues by just importing the module like a functioning human being, but this question is a python theory type of question -- i've been thinking about this one...

2 Answers2

0

this may be the dumbest of all solutions, so if anyone can explain how poor this is or if it is the recommended solution, go for it:

__init__.py:
print "hello world"

app.py:
import __init__

exec: python27 app.py

result: hello world

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 15 '22 at 08:38
0

You might want to take a look at this related question (and more importantly, the answers, really):

At which moment and how often are executed the __init__.py files by python

Generally, though, __init__.py gets executed only when the package that contains it gets imported.

Deck451
  • 61
  • 5