Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
When are imports loaded?
#1
If I have code that imports a couple of libraries I've put together, do they load to memory when the main thread starts, or when there is an actual call to the library?

Reason for asking, .I run my code and the app starts quickly, but there is a quite a pause when the libraries are first actioned.

So if I want an immediate response every time I call a library I have to make a dummy call before my processing actually begins.

Is this correct, or am I missing something about the import commands?

Thanks.
Reply
#2
(Feb-10-2019, 12:54 PM)MuntyScruntfundle Wrote: do they load to memory when the main thread starts, or when there is an actual call to the library?
Importing a module/package will always be fully imported into sys.modules mapping.
So the file/files with imported code will always be there even if you call it or not.
Eg:
# foo.py
def foo_func():
    return f'i am {foo_func.__name__}'
# bar.py
import foo

def bar_func():
    print(f'I am bar,hello also to <{foo.foo_func()}>')

if __name__ == '__main__':
    bar_func()
Output:
I am bar,hello also to <i am foo_func>
λ ptpython 
λ ptpython -i bar.py
>>> dir()
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'bar_func', 'foo', 're', 'run', 'sys']

# See that foo is also here,also the file foo.py 
>>> import sys

>>> sys.modules['foo']
<module 'foo' from 'E:\\div_code\\import_test\\foo.py'>

# So when call module foo in bar it's foo.py --> foo_func()
>>> sys.modules['foo'].foo_func()
'i am foo_func'
Reply
#3
But in terms of timing, the module is not loaded until the import statement. If bar.py was instead:

# bar.py

def bar_func():
    print(f'I am bar,hello also to <{foo.foo_func()}>')

bar_func()

import foo
You would get a name error when you tried tried to call bar_func, because foo is not loaded at that point.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is Matplotlib.pyplot Loaded DaveG 2 1,275 Apr-06-2022, 06:12 AM
Last Post: DaveG
  Imports that work with Python 3.8 fail with 3.9 and 3.10 4slam 1 2,544 Mar-11-2022, 01:50 PM
Last Post: snippsat
  Package cannot be loaded into PBS queue file emersonpl 1 1,775 Sep-09-2021, 08:06 PM
Last Post: emersonpl
  PyQt5 MySQL Drivers Not Loaded AdeS 7 4,939 Aug-06-2021, 08:34 AM
Last Post: AdeS
  Imports in my first package cuppajoeman 1 1,912 Jun-28-2021, 09:06 AM
Last Post: snippsat
  script with imports works but pytest gives "ModuleNotFoundError"? Hpao 0 1,544 Jun-27-2021, 08:30 PM
Last Post: Hpao
  Help wanted with python imports petros21 3 2,481 Apr-07-2021, 07:16 PM
Last Post: snippsat
Question How to include Modules not found (conditional imports) in my setup.py when I want to cff 0 3,765 Mar-17-2021, 11:57 AM
Last Post: cff
  MacOS BigSur Python3 - dyld: Library not loaded: trillionanswers 1 4,167 Mar-02-2021, 11:00 PM
Last Post: nilamo
  Why is mpl_toolkits loaded? Gribouillis 1 1,565 Jan-30-2021, 08:39 PM
Last Post: Serafim

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020