-2

I am trying to figure out an efficient way to initialize modules so my functions in different files can use them.

Here's an example folder structure

parent/
  main.py
  child/
    __init__.py
    utilities.py

My scripts have multiple functions that depend on many other modules. So my plan was this:

I import modules I need from the main python file.

main.py

import time
from child.utilities import *

utilities_function1()
utilities_function2()

Use the from .. import main in child functions to pull the modules from the parent file.

utilities.py

def utilities_function1():
  from .. import main
  print(time.localtime())

def utilities_function2():
  from .. import main
  print(time.localtime()+1)

However, the problem I have is that I am repeating the from .. import main for every single function in the child python file. This doesn't seem like a best practice to me so is there a more efficient way to do this?

lightstream
  • 11
  • 1
  • 3
  • It makes very little sense to import **main** everywhere... It is the main module. It shouldn't be imported anywhere... It should import other helper functions/modules. It would make more sense that `utilities.py` simply imported `time` at the top of the file... – Tomerikoo Jan 23 '22 at 14:44
  • I think your question needs more clarity, there are many ambiguities. Its hard to figure out what you are trying to do, especially because you are importing the ```main``` everywhere. Since the ```child``` folder contains ```__init__.py``` file, I assume its a module. But if so, why is it importing the ```main```? Please clarify or consider bit of restructuring. – Grasshopper Jan 23 '22 at 14:49
  • thank you @Tomerikoo ! Your recommendation is working. – lightstream Jan 23 '22 at 14:53
  • @Grasshopper i have been reading up on how to use __init__.py but don't quite understand it. Can have an 'import time' inside the __init__.py file and the functions can call it? Currently my __init__.py is empty. – lightstream Jan 23 '22 at 14:55
  • @Grasshopper i'm just trying find the best practice for importing 3rd party modules like time but I end up having to recall them everywhere. – lightstream Jan 23 '22 at 14:57
  • If you have installed third party modules using ```pip``` or the path to the modules is added in ```main``` using ```sys.path.insert(,)``` then, you can directly import the third party modules in any of the scripts inside your module (which is ```child``` here). i.e. you can directly do ```import time``` in the ```utilities.py``` file. And I think that's the best way (because even if you do multiple imports of a single module in a single file, python imports the module only once internally). – Grasshopper Jan 23 '22 at 15:06
  • @Grasshopper thank you! Yes I am using 3rd party python modules installed via `pip`. If I am understanding this correctly, I still need to call the module in `main` and then call the module again in my child file `utilities.py`. So the `import time` needs to be in both files. I'm not seeing the difference using `sys.path.insert` versus just adding a `import time` in both my python files. – lightstream Jan 23 '22 at 15:35
  • You are understanding correctly mostly. Just note that ```sys.path.insert``` isn't required for time module because its already installed in a location by pip which is searched for modules. You can use ```sys.path.insert``` to add any new locations to search for modules (if you ever need). I hope this helps :) – Grasshopper Jan 23 '22 at 18:51

1 Answers1

0

Tomerikoo's answer is working.

utilities.py

import time
def utilities_function1():
  print(time.localtime())

def utilities_function2():
  print(time.localtime()+1)

I import the time module once in the top and the subsequent functions will be able to use it.

Additional Answers

Upon further research, I found the answer around best practice on importing modules when I have a parent child folder based on these other stack post 1 and post 2.

We need to import modules that I need in every single python file no matter where the file is located (etc.: parent or child folders).

Also Python only initializes modules if needed (aka once) so I don't need to worry about accidentally importing modules too many times at least for efficiency sake.

lightstream
  • 11
  • 1
  • 3