63

In a module residing inside a package, i have the need to use a function defined within the __init__.py of that package. how can i import the package within the module that resides within the package, so i can use that function?

Importing __init__ inside the module will not import the package, but instead a module named __init__, leading to two copies of things with different names...

Is there a pythonic way to do this?

CharlesB
91.3k29 gold badges203 silver badges228 bronze badges
asked Jan 12, 2009 at 18:38

5 Answers 5

48

Also, starting in Python 2.5, relative imports are possible. e.g.:

from . import foo

Quoting from http://docs.python.org/tutorial/modules.html#intra-package-references:


Starting with Python 2.5, in addition to the implicit relative imports described above, you can write explicit relative imports with the from module import name form of import statement. These explicit relative imports use leading dots to indicate the current and parent packages involved in the relative import. From the surrounding module for example, you might use:

from . import echo
from .. import formats
from ..filters import equalizer
answered Jan 12, 2009 at 19:52
Sign up to request clarification or add additional context in comments.

2 Comments

This does not answer the question of how to import from __init__.py.
@BrenBarn: yet this is the way to import it; when you import the package, the __init__.py file serves as the effective namespace. You should never import __init__.py directly.
23

This doesn't exactly answer your question, but I'm going to suggest that you move the function outside of the __init__.py file, and into another module inside that package. You can then easily import that function into your other module. If you want, you can have an import statement in the __init__.py file that will import that function (when the package is imported) as well.

answered Jan 12, 2009 at 18:40

4 Comments

i realise i can do this, it just seems rather messy and um, unpythonic.
Not sure how this is 'unpythonic'? Wouldn't the alternative be even more messy, since init.py is not really a module?
+1 - using a common.py within your package is very usual practice in Python.
I've been messing around and haven't found any better way
5

If the package is named testmod and your init file is therefore testmod/__init__.py and your module within the package is submod.py then from within submod.py file, you should just be able to say import testmod and use whatever you want that's defined in testmod.

answered Jan 12, 2009 at 19:13

Comments

1

I'm not totally sure what the situation is, but this may solve your "different name" problem:

import __init__ as top
top.some_function()

Or maybe?:

from __init__ import some_function
some_function()
answered Jan 12, 2009 at 19:26

3 Comments

This does work, but at least in cPython 2.7 the interpreter considers __init__ to be a different module than top, and so the __init__.py file gets loaded twice.
This is an implicit relative import, and as such won't work in python 3, and is bad practice in python 2 anyway.
This still creates sys.modules['__init__']. Basically, the __init__.py module should never be directly imported. Import the package (the directory name) instead.
1

In Django, the file manage.py has from django.core.management import execute_manager, but execute_manager is not a module. It is a function within the __init__.py module of the management directory.

Esteban Küber
37k15 gold badges82 silver badges97 bronze badges
answered Apr 2, 2009 at 2:30

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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