Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. If that’s the case, Python uses the existing module object as is.

Solution 3:

Got it! Okay I found what I was looking for. I just found it clunky to have to re-import code I already imported, especially when the file isn't a library or something, just split up code. found it here: http://norvig.com/python-lisp.html

execfile("file.py")

Answers my problem perfectly. It parses a file and executes the code in it. Using it I was able to take out the extra import statements and it runs perfectly :D

Post a Comment for "Recalling Import In Module"