0

I'm new to Python so I'm sorry if this question seems dumb, but I couldn't find specific help on that.

I'm working on a testing project where I wanted all the logic to be loaded into a mother component so they could be immediately accessed by children modules. Is it possible in Python language?

For example, if I'm creating a package with this tree:

game/
  __init__.py
  start.py

  core/
    ...
    config.py

  mechanics/
    ...
    combat/
      ...

and if I use, for example, random library in almost every module, is it necessary to import it to each individual file or can i just import it into start.py so it could be inherited by children modules?

My ideal goal was to use core/config.py to import all the basic and shared logics and just importing this config file to all children modules, but it is not working.

Any ideas? Is there any better way to do that? Thanks a lot!

Анна
  • 1,248
  • 15
  • 26
  • 2
    Each module needs to import the libraries it uses. However, Python automatically caches each module in the `sys.modules` dictionary so if another script imports it later, the value in the cache is used—so it's very fast. – martineau Feb 23 '19 at 08:17
  • 1
    Also, for a package, you can auto-import all the sub-modules you want in the `__init__.py` file to make them appear to be part of the main package. Here's a some answers about doing something like that automatically: [How to import members of all modules within a package?](https://stackoverflow.com/questions/14426574/how-to-import-members-of-all-modules-within-a-package/14428820#14428820) and [Importing classes from different files in a subdirectory](https://stackoverflow.com/questions/5134893/importing-classes-from-different-files-in-a-subdirectory/5135444#5135444). – martineau Feb 23 '19 at 08:31

0 Answers0