Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recalling import in module

I'm still learning python and after playing around with pygame I noticed I'm re-importing things in modules I'm importing that I've already imported.

import pygame

For instance I have some classes in a separate file, but I must also import pygame into that file too for them to work. Does it actually import the code twice? Will it slow down my program? Or does it just pull the same import from cache, but if it does that, why would I need to import it again?

Is there anything like (load) in lisp that just pulls in the code like it is part of the main file?

Thank You

like image 983
Isaiah Avatar asked Jun 27 '26 19:06

Isaiah


1 Answers

Subsequent imports pull the cached module reference from sys.modules. You need to import in order to add the module to the current namespace/scope.

like image 198
Ignacio Vazquez-Abrams Avatar answered Jun 30 '26 08:06

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!