5

I want to create a Python package that has multiple subpackages. Each of those subpackages contain files that import the same specific module that is quite large in size.

So as an example, file A.py from subpackage A will import a module that is supposedly named LargeSizedModule and file B.py from subpackage B will also import LargeSizedModule. Similarly with C.py from subpackage C.

Does anyone know how I can efficiently import the same exact module across multiple subpackages? I would like to reduce the 'loading' time that comes from those duplicate imports.

3
  • 2
    There is no loading time from duplicate imports; the import itself happens once, then it's just a lookup (in sys.modules, IIRC). What has made you think that this is a problem? Commented Nov 17, 2015 at 15:51
  • @jonrsharpe I see. I just started building the barebones of my package and wanted to prevent something like this from making me overhaul the entire package structure if it becomes a possible efficiency issue later on. I am newb :) Thanks. For other newbs this link might make good reading on the subject: docs.python.org/2/reference/… Commented Nov 17, 2015 at 16:01
  • See also stackoverflow.com/questions/29504313/… Commented Nov 17, 2015 at 16:09

1 Answer 1

7

By doing import LargeSizedModule everywhere you need it. Python will only load it once.

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

1 Comment

@TomW To add to this, the only time Python will re-import something is if you call reload(LargeSizedModule) on it or use your own importlib.import_module() and give it a different common name.

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.