2

I might be completely wrong here, but I can't find a proper google source for the dilemma that I have:

Let's say we are using python, and we have files

foo.py and bar.py, which have the following pseudocode:

Code in foo.py:

# Code in foo.py
import sys
def foo():
# Some blah code for foo function

And code in bar.py is:

# Code in bar.py
import sys
import foo
def bar():
# Some blah code for bar function

Now, what I am wondering is : Will this not cause code bloat?, since we have imported sys twice in bar.py. Once via import sys and another time because we are doing import foo?

Additionally, what will be the correct thing to do when you have to include libraries in multiple files, which in turn will be included in other files?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
joshi
  • 355
  • 1
  • 5
  • 21
  • 1
    Within a single interpreter session, python modules are only imported once (unless you're trying very hard to do something different). Modules (and the python packaging ecosystem are what makes you more productive then is other less dynamic environments - ie/ do this more, not less) – Andrew Walker Aug 13 '15 at 10:53
  • Thanks Andrew, but I guess I am asking for what is the most elegant way to not do something like above? What is pythonic way? Sorry, I moved from C++ and trying to grasp the basic philosophy here... – joshi Aug 13 '15 at 10:54
  • No, they get loaded once (at the first import site) and then shared amongst all the other uses – Andrew Walker Aug 13 '15 at 10:56
  • @joshi There's no real reason to avoid doing what you've done if you are also using `sys` directly in `bar.py`. Note that you don't need to import `sys` in `bar.py` just so your `foo` import will work, however. – chucksmash Aug 13 '15 at 11:00
  • possible duplicate of [what happens when i import module twice in python](http://stackoverflow.com/questions/19077381/what-happens-when-i-import-module-twice-in-python) – tripleee Aug 13 '15 at 11:08
  • Thank you everyone! This was enlightening! – joshi Aug 13 '15 at 12:01

2 Answers2

3

Importing a module twice in python does not introduce "bloat". A second import is a mere name-lookup in a cached modules-dictionary (sys.modules, to be precise. Which in case of sys makes this even less relevant, as there is actually nothing that doesn't implicitly trigger an import of sys - although it's obviously not exposed in the namespace).

And what happens if you import some parts of a module x in foo.py, and need them and possibly others in bar.py? Having to carefully groom your imports, and then use foo.something_from_x or x.something_else_from_x in bar.py would be extremely cumbersome to write and maintain.

TLDR: don't worry. Really. Don't.

deets
  • 6,285
  • 29
  • 28
  • Thanks a lot! This was enlightening! – joshi Aug 13 '15 at 12:02
  • @joshi because you mentioned your C++-background: In my C++-work, I strive for completeness of includes per compilation unit. Granted, you often miss std::vector as it's being reined in from some other include. But I want to reason about a piece of code in front of me without mentally juggling the dependent headers as well. Same thing applies in python. – deets Aug 13 '15 at 13:40
2

This would not cause any kind of code bloat . When you import the same library multiple times , python only actually imports it one time (the first time) , and then caches it in sys.modules , and then later on everytime you do import sys , it returns the module object from sys.modules.

A very simple example to show this -

Lets say I have an a.py -

print("In A")

This would print In A everytime the module is imported. Now lets try to import this in multiple times -

>>> import a
In A
>>> import a
>>> import a
>>> import a

As you can see the code was imported only once actually, the rest of the times the cached object was returned. To check sys.modules -

>>> import sys
>>> sys.modules['a']
<module 'a' from '\\path\to\\a.py'>

When you import a module, what happens is that python imports the code , and creates a module object and then creates a name in the local namespace with either the name of the module (if no as keyword was provided , otherwise the name provided after as keyword) , and assigns the module object to it.

The same thing happens when doing it when importing in other modules. Another example -

b.py -

import a

print("In B")

c.py -

import b
import a

print("In C")

Result of running c.py -

In A
In B
In C

As you can see , a.py was only imported once.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176