91

Suppose I have a module foo.py and a package foo/. If I call

import foo

which one will be loaded? How can I specify I want to load the module, or the package?

1
  • 46
    pythons import system is an unmitigated and ongoing travesty.
    – Todd Freed
    Commented May 31, 2018 at 21:28

3 Answers 3

34

I believe the package will always get loaded. You can't work around this, as far as I know. So change either the package or the module name. Docs: http://docs.python.org/tutorial/modules.html#the-module-search-path

3
  • 5
    Hi, can you please provide a reference for your claim? I don't see anything in the documentation link provided that says the package will always be loaded in preference to the module.
    – wim
    Commented Apr 21, 2016 at 1:59
  • 4
    This answer is incorrect. The linked docs (for both Python 2 and 3) state that the directory of the current script is given preference in the search order. See also: python-notes.curiousefficiency.org/en/latest/python_concepts/… Commented Apr 10, 2019 at 20:18
  • 1
    While docs says package is preferred over module in my experience it's not true, at least in python 2.7. It seems like in Python 2.7 import seeks for the module at parent (but not child) directories until it finds it, so installed packages will be checked at the end of resolving.
    – Qback
    Commented Apr 8, 2020 at 11:03
20

Actually, it is possible, by manually guiding the import machinery to use a .py file instead of directory. (This code is not well tested, but seems to work). UPDATE 2020: Note that this requires using custom import_module() function instead of normal import statement. However, with modern Python3 and its importlib, it might be possible to make the bare import statement to work the same way too. (Note that this answer shows flexibility which Python offers. It's not an encouragement to use this in your applications. Use this only if you know what you're doing.)

File foo.py

print "foo module loaded"

File foo/__init__.py

print "foo package loaded"

File test1.py

import foo

File test2.py

import os, imp

def import_module(dir, name):
    """ load a module (not a package) with a given name 
        from the specified directory 
    """
    for description in imp.get_suffixes():
        (suffix, mode, type) = description
        if not suffix.startswith('.py'): continue
        abs_path = os.path.join(dir, name + suffix)
        if not os.path.exists(abs_path): continue
        fh = open(abs_path)
        return imp.load_module(name, fh, abs_path, (description))

import_module('.', 'foo')

Running

$ python test1.py 
foo package loaded

$ python test2.py 
foo module loaded
2
  • 1
    So, it's possible by manually hacking how imports are processed. That's great, and a worthy answer. But note that by manual hacking, almost anything is possible. So, I'm inclined to edit this answer, to emphasize "manual hacking" aspect.
    – pfalcon
    Commented Dec 26, 2020 at 9:11
  • Also note that hacking at the proper level (importlib import hooks), it might be possible to make the bare import foo to have a preference for foo.py the module instead of foo/ the package.
    – pfalcon
    Commented Dec 26, 2020 at 9:12
15

Maybe you want to move your classes from foo.py module to __init__.py.

This way you'll be able to import them from the package as well as importing optional subpackages:

File foo/__init__.py:

class Bar(object):
...

File foo/subfoo.py:

class SubBar(object):
...

File mymodule.py:

from foo import Bar
from foo.subfoo import SubBar
3
  • 2
    I would like to know if it is ok to do this too.
    – Parham
    Commented Jan 21, 2013 at 8:40
  • 3
    Lennart Regebro recommended it in another answer so it is okay: stackoverflow.com/a/17530651/992687 Commented Jul 1, 2015 at 9:20
  • simple, elegant and probably the only python-ish solution here Commented Feb 18, 2021 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.