Reputation: 21510
If this is my directory tree
temp
├── __init__.py
└── __main__.py
0 directories, 2 files
And I have the following code in __init__.py
and in __main__.py
"""Initializes the module"""
CONSTANT = 1
sys.exit("what is happening here")
# from . import CONSTANT
# from temp import CONSTANT
if __name__ == "__main__":
print "This should never run"
I am getting two problems here that I am trying to figure out
On running python .
in the temp
directory I get the output
This should never run
, shouldn't the module be initialized first with the __init__.py
file resulting in the abort?
Second how do I go about doing imports in python modules? Neither of the two options I have mentioned above works. I can neither do from . import CONSTANT
nor from temp import CONSTANT
in the code above. What is the right way to do relative imports?
I am running this on Python 2.7.5, apologies if this has already been asked before.
Upvotes: 1
Views: 369
Reputation: 44444
You should be running it from out of the temp
directory. If someDir
contains your temp
directory, then:
someDir $ python -m temp #someDir/temp/__init__.py is your file.
On running python . in the temp directory I get the output This should never run, shouldn't the module be initialized first with the init.py file resulting in the abort?
If you run it from outside, __init__.py
will be called. And sys.exit
will be called too.
Second how do I go about doing imports in python modules? Neither of the two options I have mentioned above works. I can neither do from . import CONSTANT nor from temp import CONSTANT in the code above. What is the right way to do relative imports?
You are doing it just fine. Just import sys in your __init__.py
file. And fix the spelling of CONSTANT
.
Also why do I need the -m flag? Isn't it ok to just do python temp from the parent directory of temp?
You need the -m
flag to tell that you are using packages. If you dont use it you wont be able to do relative imports.
Upvotes: 1
Reputation: 1121972
You are running inside temp
; this is not considered a package and __init__.py
is not loaded. Only if the parent of the current directory is on the module loading path and you explicitly load temp
as a module, is __init__.py
loaded.
Because temp
is not a package you can't use relative imports here. Instead, every Python file inside of the directory is considered a top-level module all by themselves.
You'd have move to the parent of the temp
directory, then run:
python -m temp
for Python to import temp
as a package and then run the __main__
module in that package.
Upvotes: 1
Reputation: 280778
When you tell Python to run a directory, Python does not treat the directory as a package. Instead, Python adds that directory to sys.path
and runs its __main__.py
. __init__.py
is not executed, and relative imports will not view the directory as a package.
If you want to run a package's __main__.py
and treat it as part of the package, with __init__.py
executed and all, go to the directory containing the package and run
python -m packagename
Upvotes: 1