remort
remort

Reputation: 314

make imported modules accessible for further imported modules

There is a main program importing a module with classes or something usefull that another submodule shall use too. For example:

main.py: import datetime datetime.now() import mod

mod.py: datetime.today()

When importing 'mod' module python gives an error that 'datetime' is not defined. datetime.today() cant be executed.

What should I do if I need to create a modular app in python instead of one-file apllication program? Should I always repeat my imports at the head of each module file ? Or can I make imported modules accessible from further imported modules?

Upvotes: 0

Views: 87

Answers (1)

BrenBarn
BrenBarn

Reputation: 251368

Should I always repeat my imports at the head of each module file?

Yes. Every module needs to import what it needs to use.

As two great minds noted in the comments, the actual loading of the module only takes place once. Multiple imports will reuse the already-loaded module, so it won't have any significant performance impact.

Upvotes: 2

Related Questions