13

Suppose I have the following structure:

app/
  __init__.py
  foo/
    a.py
    b.py
    c.py
    __init__.py

a.py, b.py and c.py share some common imports (logging, os, re, etc). Is it possible to import these three or four common modules from the __init__.py file so I don't have to import them in every one of the files?

Edit: My goal is to avoid having to import 5-6 modules in each file and it's not related to performance reasons.

4
  • If you want to do this for performance reasons, don't worry - importing already loaded modules is super-fast (a simple dict lookup on sys.modules). Commented Jul 29, 2009 at 20:25
  • Edited question to clarify my motives. Commented Jul 31, 2009 at 15:53
  • 1
    The stated goal kinda reduces code readability, don't you think? Commented May 26, 2010 at 23:40
  • 1
    The fundamental problem is that Python has no package scope, only module scope. Packages really aren't first-class constructs in Python, they were sort of bolted on later, and all they really do is allow dotted notation of modules. Maybe create a PEP for it? Commented May 26, 2010 at 23:52

3 Answers 3

14

You can do this using a common file such as include.py, but it goes against recommended practices because it involves a wildcard import. Consider the following files:

app/
    __init__.py
foo/
    a.py
    b.py
    c.py
    include.py <- put the includes here.
    __init__.py

Now, in a.py, etc., do:

from include import *

As stated above, it's not recommended because wildcard-imports are discouraged.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 for wildcards. A foolish consistency is the hobgoblin of little minds. The inability to group related imports sensibly is a python wart; thus, it must yield to the wildcard.
11

No, they have to be put in each module's namespace, so you have to import them somehow (unless you pass logging around as a function argument, which would be a weird way to do things, to say the least).

But the modules are only imported once anyway (and then put into the a, b, and c namespaces), so don't worry about using too much memory or something like that.

You can of course put them into a separate module and import that into each a, b, and c, but this separate module would still have to be imported everytime.

Comments

6

Yes, but don't do it. Seriously, don't. But if you still want to know how to do it, it'd look like this:

import __init__

re = __init__.re
logging = __init__.logging
os = __init__.os

I say not to do it not only because it's messy and pointless, but also because your package isn't really supposed to use __init__.py like that. It's package initialization code.

1 Comment

I wouldn't say it's pointless. Some things you want to apply to all your code, e.g. from future import unicode_literals. Do I really have to put that in every file (and then forget some, resulting in hard-to-find bugs)?

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.