15

Suppose in the current directory there is a file named somecode.py, and a directory named somecode which contains an __init__.py file. Now I run some other Python script from this directory which executes import somecode. Which file will be imported - somecode.py or somecode/__init__.py?

Is there even a defined and reliable search order in which this is resolved?

Oh, and does anyone have a reference to official documentation for this behavior? :-)

CharlesB
  • 86,532
  • 28
  • 194
  • 218
oliver
  • 153
  • 3
  • Btw. this is not a duplicate of http://stackoverflow.com/questions/4092395/python-import-precedence-packages-or-modules because that post has a slightly different question, and the accepted answer cannot be applied to my question. – oliver May 18 '11 at 19:13
  • You're going to have to add to your question to clarify, then; I've read these both twice, and it looks like an exact dupe to me. The accepted answer there even gives the text from the docs that you asked for. – jscs May 18 '11 at 22:06
  • 1
    @Josh Caswell: Well, Case 1 from that question does look exactly like this one, but the accepted answer doesn't really say anything about it, or it says the wrong thing. (You really have to read the whole docs to get the context; that passage appears before packages are even mentioned at all.) What *does* look like an applicable answer is the one from kanaka (which had no votes until I voted it up just now). It addresses the question but does not cite documentation. – John Y May 18 '11 at 22:22
  • https://stackoverflow.com/q/58686448/674039 – wim Nov 04 '19 at 06:19

2 Answers2

11

Packages will be imported before modules. Illustrated:

% tree .
.
|-- foo
|   |-- __init__.py
|   `-- __init__.pyc
`-- foo.py

foo.py:

% cat foo.py 
print 'you have imported foo.py'

foo/__init__.py:

% cat foo/__init__.py
print 'you have imported foo/__init__.py'

And from interactive interpreter:

>>> import foo
you have imported foo/__init__.py

I have no idea where this is officially documented.

Edit per comment: This was performed with Python 2.7 on Mac OS X 10.6.7. I also performed this using Python 2.6.5 on Ubuntu 10.10 and experienced the same result.

jathanism
  • 33,067
  • 9
  • 68
  • 86
  • 2
    +1 for having tried, however, this doesn't prove it's a defined behaviour or luck. You might want to complete with exact platform you tried on. – Bruce May 18 '11 at 21:05
  • You're right. I suspect that it has something to do with the way files are displayed by default when a directory listing is performed. – jathanism May 18 '11 at 22:55
  • Thanks, I suppose that answer is good enough for me... If it turns out that this ordering is not reliable, I'll notice when bug reports start rolling in :-) – oliver May 19 '11 at 19:26
  • You're welcome! I'd be interested to see the results of this on Windows, but don't have ready access to a Windows machine. – jathanism May 19 '11 at 21:44
  • 2
    This is documented at http://www.python.org/doc/essays/packages.html - see "What If I Have a Module and a Package With The Same Name?" – Peter Cock Aug 14 '12 at 17:47
  • @jathanism, see post below – alpha_989 Feb 13 '18 at 19:07
  • @peterjc Link is broken, new home is http://www.python.org/doc/essays/packages/ – wim Nov 04 '19 at 04:23
-1

tested in Windows 10 (Python version 3.5) and on Ubuntu Linux (Python version 2.7 and 3.5) using the following directory:

https://github.com/alphaCTzo7G/stackexchange/tree/master/python/order_import_module_vs_package

Result

The module is always loaded and the following message is printed out every time:

"you have imported foo/init.py"

So it seems to be consistent across these Systems, that the package is loaded first.

alpha_989
  • 4,882
  • 2
  • 37
  • 48