4

I have a package MyPak and a module MyMod. The files are organized in the following way:

somedir/MyPak/MyMod.py

in MyMod.py there is only a Class whose name is also MyMod

in directory somedir/MyPak, if I do the import:

import MyMod

I got MyMod imported as a Module.

But in directory somedir/, if I do

from MyPak import MyMod

I got MyMod imported as a Class, not a Module. Why is this the case?

HongboZhu
  • 4,442
  • 3
  • 27
  • 33

1 Answers1

4

This behavior is indicative that you have a file:

somedir/MyPak/__init__.py

wherein you do the following:

from MyMod import *

When you import MyPak, it imports from that __init__.py - likewise, when you from MyPak import something, it's going to try to pull from the namespace for the package - which will look inside that __init__.py

Because you imported everything from MyMod inside __init__.py, now the class is local to the MyPak package, and masks the MyMod.py file.

Nate
  • 12,499
  • 5
  • 45
  • 60
  • That's true. I did find the file \__init\__.py which contains `from MyMod import MyMod`. But which masks which? I thought \__init\__.py is first executed (MyMod because a class) and then the `import MyMod` part from the code `from MyPak import MyMod` is executed (this masks the previous result and makes MyMod a module again. – HongboZhu Aug 04 '11 at 17:21
  • Forgot to say thanks :) How about other modules in the directory somedir/MyPak ? Because I see also `from MyMod2 import MyMod2`; `from MyMod3 import MyMod3` and so on in the file \__init\__.py. If somedir/MyPak/\__init\__.py is executed when I do `from MyPak import MyMod`, then where have MyMod2, MyMod3, etc been imported? – HongboZhu Aug 04 '11 at 17:31
  • **re your first comment**: Nope. The line `from MyPak import MyMod` will first attempt to import the `MyMod` symbol from `MyPak/__init__.py`'s namespace, and only failing that will it attempt to import some file `MyPak/MyMod.py`. Because you **have** imported that symbol into `MyPak`'s namespace, it will be imported directly from there. – Nate Aug 04 '11 at 17:32
  • **re your 2nd comment**: They were not imported :^( You need to either import each specifically, *OR*, just `import MyPak` (or `from MyPak import *`, which gets everything). ALTERNATIVELY, you can say `from MyPak.MyMod import MyMod`, and cut out the middleman! – Nate Aug 04 '11 at 17:38
  • I don't quite understand the part that " ... attempt to import the MyMod symbol from MyPak/__init__.py's namespace" but the rest lines in MyPak/\_\_init\_\_.py are not executed. Could you point me to some further documents explaining this issue? – HongboZhu Aug 04 '11 at 17:43
  • I want to import MyMod as **module** particularly because I want to make some changes to the code MyMod.py and test the changes in python shell by reloading the module. But if it is imported as a Class, how should I reload it without restarting Python shell? (perhaps I should ask it in a new question :) – HongboZhu Aug 04 '11 at 17:47
  • [here's a link regarding package namespace and `__init__.py`](http://stackoverflow.com/questions/119167/adding-code-to-init-py). And I encourage you to ask a new question, as this is quite a discussion here in the comments. – Nate Aug 04 '11 at 17:51