When is it beneficial to use inline, single use importing in Python?

For example:


Is the above ever shorter than the below?

import x


from x import*

3 Answers 3


This can be useful if you want to use a module just once in an anonymous lambda function, as it allows you to avoid writing a separate statement:

lambda x:__import__('SomeModule').foo(x,123)

is one byte shorter than

from SomeModule import*;f=lambda x:foo(x,123)

If the code is a named function or program, then __import__ is unlikely to help except in the most extreme or contrived circumstances.

  • \$\begingroup\$ __import__ would help inside of a named function since it would be inside of an indented block. An import would cost extra since it's on two lines and indented. (That's assuming that imports aren't allowed outside of the function.) \$\endgroup\$
    – Alex A.
    Commented Aug 2, 2015 at 18:01
  • \$\begingroup\$ @AlexA I see no problem with placing imports outside of a named function. \$\endgroup\$
    – feersum
    Commented Aug 2, 2015 at 18:03
  • \$\begingroup\$ Then would there ever be a time when import would be called inside of an indented block? Probably not. \$\endgroup\$
    – Alex A.
    Commented Aug 2, 2015 at 18:05
  • \$\begingroup\$ placing imports outside of the named function won't work for challenges where you're writing just one function. \$\endgroup\$
    – Sparr
    Commented Aug 2, 2015 at 19:53
  • 6
    \$\begingroup\$ The second example doesn't require the f= because anonymous functions are allowed and can have content outside the function. \$\endgroup\$
    – xnor
    Commented Aug 2, 2015 at 20:25

When importing multiple modules with sufficiently long names, it can be helpful to assign the __import__ function to a shorter variable and use it for imports


Regular import statements - 97 bytes

from itertools import*
from datetime import*
print list(permutations("abc"))
print datetime.now()

Assigning __import__ to i - 94 bytes:

print list(i("itertools").permutations("abc"))
print i("datetime").datetime.now()

Almost never.

__import__("x").doSomething() needs 15+x characters to refer to a module with a name of length x.

import x\nx.doSomething() needs 9+2*x characters. These functions overlap at x=6, so compared to this way any module with a longer name is better off using __import__, anything shorter benefits from normal imports:

However, from x import*\ndoSomething() needs only 14+x characters, so compared to the normal import it is not worth it unless the module name is longer than 5 characters:

This all assumes that you are referring to a function/class/whatever only once. If you refer to it more than once, the formulas change, and the latter version might become the winner. In case you use something long from an imported module several times, yet another version wins:

from x import y as z nets you 18+x+y+z*(n+1) characters for n uses of z, which is a large improvement if y is big, because z can be made 1.

  • 1
    \$\begingroup\$ Can you check your math? I find __import__("x").doSomething() to be one char shorter than from x import*;doSomething(). Maybe you're counting the newline as two chars? And I recall the break-even point being 5, falling between time and random. \$\endgroup\$
    – xnor
    Commented May 7, 2017 at 9:25
  • \$\begingroup\$ @xnor I didn't count newlines twice, but I missed that you don't need the dot in that version. Give me a minute, I'll update. \$\endgroup\$
    – L3viathan
    Commented May 7, 2017 at 9:28
  • 1
    \$\begingroup\$ z=__import__("x").y is a byte shorter than from x import y as z \$\endgroup\$ Commented Jun 18, 2019 at 13:06
  • 1
    \$\begingroup\$ @negativeseven Shorter: from x import*;z=y. \$\endgroup\$
    – L3viathan
    Commented Jun 18, 2019 at 14:05

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.