0

Is there a downside to importing the same built-in module in both my script and custom module?

I have a script that: imports my custom module and imports the built-in csv module to open a csv file and append any necessary content to a list.

I then have a method in my custom module in which i pass a path, filename and list and writes a csv, but I have to import the csv module again (in my module).

I do not understand what happens when I import the csv module twice so I wanted to know if there is a more uniformed way of doing what I'm doing or if this is ok.

jes516
  • 542
  • 1
  • 13
  • 30
  • Does [this](http://stackoverflow.com/questions/2029523/how-to-prevent-a-module-from-being-imported-twice) solve your problem? – Zach P May 20 '15 at 20:53
  • No, it's fine; Python only actually imports each module once – jonrsharpe May 20 '15 at 20:53
  • See ['import module' vs. 'from module import function'](https://programmers.stackexchange.com/q/187403); importing loads the file **once**, then just binds names to loaded objects. – Martijn Pieters May 20 '15 at 20:54

3 Answers3

3

No, there is no downside. Importing a module does two things:

  1. If not yet in memory, load the module, storing the resulting object in sys.modules.
  2. Bind names to either the module object (import modulename) or to attributes of the module object (from modulename import objectname).

Additional imports only execute step 2, as the module is already loaded.

See The import system in the Python reference documentation for the nitty gritty details:

The import statement combines two operations; it searches for the named module, then it binds the results of that search to a name in the local scope.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
2

The short answer is no, there is no downside.

That being said, it may be helpful to understand what imports mean, particularly for anyone new to programming or coming from a different language background.

I imagine your code looks something like this:

# my_module.py
import os
import csv

def bar(path, filename, list):
    full_path = os.path.join(path, filename)
    with open(full_path, 'w') as f:
        csv_writer = csv.writer
        csv_writer.writerows(list)

and

# my_script.py
import csv
import my_module

def foo(path):
    contents = []
    with open(path, 'r') as f:
        csv_reader = csv.reader(f)
        for row in csv_reader:
            contents.append(row)

As a high-level overview, when you do an import in this manner, Python determines whether the module has already been imported. If not, then it searches the Python path to determine where the imported module lives on the file system, then it loads the imported module's code into memory and executes it. The interpreter takes all objects that are created during the execution of the imported module and makes them attributes on a new module object that the interpreter creates. Then the interpreter stores this module object into a dictionary-like structure that maps the module name to the module object. Finally, the interpreter brings the imported module's name into the importing module's scope.

This has some interesting consequences. For example, it means that you could simply use my_module.csv to access the csv module within my_script.py. It also means that importing csv in both is trivial and is probably the clearest thing you can do.

One very interesting consequence is that if any statements that get executed during import have any side effects, those side effects will only happen when the module is first loaded by the interpreter. For example, suppose you had two modules a.py and b.py with the following code:

# a.py
print('hello world')

# b.py
print('goodbye world')
import a

If you run import a followed by import b then you will see

>>> import a
hello world
>>> import b
goodbye world
>>>

However, if you import in the opposite order, you get this:

>>> import b
goodbye world
hello world
>>> import a
>>>

Anyway, I think I've rambled enough and I hope I've adequately answered the question while giving some background. If this is at all interesting, I'd recommend Allison Kaptur's PyCon 2014 talk about import.

Andrew
  • 669
  • 7
  • 16
  • 1
    Another interesting note is that the module name in your current module is a variable like any other. It can be *reassigned* to something else; this is especially useful for mocking. +1 – jpmc26 May 21 '15 at 15:30
0

You can import the same module in separate files (custom modules) as far as I know. Python keeps track of already imported modules and knows how to resolve a second import.

junnytony
  • 3,455
  • 1
  • 22
  • 24