As the title says, do I need to reimport a module in a module that I imported to my main file? Can’t find anything on this and not really sure what to search for either. Cause this don’t work until I import otherepic in epicness
file1:
import epicness
import otherepic
epicness.someother(3)
epicness:
def someother(x):
return dosomething(x)
otherepic:
def dosomething(x):
return x*4
Yes, you have to import everything you need in every module. If in
module_ayou use the functionBdefined inmodule_bthen you must importmodule_binsidemodule_a, or at least import theBfunction frommodule_b.Explanation:
In Python modules are objects! When you import a module its code is executed and everything that gets defined there is attached to the module object’s
__dict__:The module’s
__dict__contains also the usual global built-ins.Anything that is defined inside a module uses the module’s
__dict__as global scope.In python there is no such a thing as a “global variable” meaning a variable accessible from every module/class/function. Globals variables are actually just a module’s instant variables.
If you want to import some items from a module into an other module’s namespace you can use the
fromsyntax:You can import everything using the
*:But avoid using the
from ... import *syntax! You’ll get namespacing clashes like in C includes. Only do this if a module states in its documentation that it is*-import safe.To make a module
*-import safe you can define the__all__global, which should be a sequence of strings representing the identifiers that should be exported when a*-import is executed.For example: