4

How do I import and run a Python function and have all the dependencies it uses use the imports from the main Python file?

Main Python file:

from im import er
import time
er()

Python file with function to be imported:

def er():
    time.sleep(1)
    print('hi')

This doesn't work because the time module is not imported in the im.py. How can I make this work without importing the modules it needs each time I run the function?

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
  • No,time module must be imported in im.py – jizhihaoSAMA Feb 19 '20 at 14:40
  • Yes the time module was just an example but I dont want to have to import all the modules I need each time I run a function because it is time critical. If I define the function in the main python file I dont have to import the functions each time I run the function. –  Feb 19 '20 at 14:42
  • 1
    Why do you want to do this? – jizhihaoSAMA Feb 19 '20 at 14:44
  • 1
    Why *don't* you want to import the dependencies in the module defining ``er``? It knows much better than the main module what dependencies there are. – MisterMiyagi Feb 19 '20 at 14:45
  • 1
    @fiddlewee Can you clarify why your thing your approach means you "dont have to import the functions each time I run the function"? Modules are imported *once* for the entire interpreter session, they are name-bound *once* for each ``import`` statement. Calling a function that does not execute ``import`` will not import anything. Even if it does ``import`` something, running it again will only do the (very cheap) name binding. – MisterMiyagi Feb 19 '20 at 14:47

1 Answers1

4

You have to import the function in the main, and required module for the function in the function file.

Main Python file:

from im import er

er()

Imported module :

from time import sleep

def er():
    sleep(1)
    print('hi')

This behave this way because Python run the imported module when you import it. Then, depending of your import statement, it will do:

  • import <module>: create a module object named <module> with attributes allowing you to access global scope of this module. If a function is in the global scope of the module, you access it with <module>.<function_name>.
  • from <module> import *: add to the global scope of the current module the global scope of the imported module (not exactly, but if you need more informations about it, look for wildcard import behaviour). As a good rule of thumb with this import: don't use this.
  • from <module> import <symbol>: add the symbol of the imported module to the global scope of the current module.

More info about imports:

Dorian Turba
  • 3,260
  • 3
  • 23
  • 67
  • Thank you this works but would it be possible to not import anything in the imported module if the time module is already imported in the main python file? I dont want to import the same module two times for when I also want to use the time module in the main python file. –  Feb 19 '20 at 15:06
  • I recommend to use `from import `, in order to import only required code and not the entire module. But to answer your question: no. Trust Python, import where you use it, and only there. – Dorian Turba Feb 19 '20 at 15:08
  • Any idea why this works? My understanding was that imports (at least in c) work by just copying all the text from one file into another. If so, would it not just copy the text from one function into the main script? – benjamin deworsop Apr 11 '22 at 04:51
  • 1
    @benjamindeworsop see my edit and tell me if that's better :) – Dorian Turba Apr 11 '22 at 09:38
  • 1
    This is not *entirely* true. Modules are only ever imported once; subsequent `import` statements will not run the module again, they'll at most bind it to a new name. Also, `from __ import __` *still runs the entire module* (once), the only thing that changes is what is bound to what name. And Python imports very differently from C. – CrazyChucky Apr 11 '22 at 09:44
  • @CrazyChucky Good point, but I never said that the code is run at "each import" ;) feel free to add this precision to the answer. IMO, this isn't very important in this case, but good to know for beginners. – Dorian Turba Apr 11 '22 at 10:06
  • Cool, reading this and the docs a little it seems the creation of the "module" object handles some of the seemingly magic behaviour. Thanks! – benjamin deworsop Apr 12 '22 at 00:28