2

I am importing a module named as dependency.py inside another module app.py. I am curious to know that when I compile app.py into app.pyc, does app.pyc include the byte code of relevant code segment from dependency.py ? Or does app.pyc include an instruction to open and read from dependency.py?

app.py is as follows:

 from dependency import myfunc
 print("inside app.py")
 myfunc()

dependency.py is as follows:

def myfunc():
      print("inside dependency")

Now there are two possible scenarios:

scenario 1. app.pyc looks like

byte code corresponding to <print("inside app.py")>
byte code corresponding to <print("inside dependency")>

scenario 2. app.pyc looks like

byte code corresponding to <print("inside dependency")>
byte code corresponding to the instruction <open dependency.py and then read what myfunc means>

I tried to run app.pyc after deleting dependency.py and it throws error saying - ModuleNotFoundError: No module named 'dependency'. So it seems that scenario 2 is true. But I don't have a cs background and new in programming world, So I'm asking you if I am right or something different is happening?

Rambo
  • 29
  • 2
  • I'm not sure why you bring bytecode into this. `from dependency import myfunc` executes `dependency.py` and inserts the name `myfunc` into the global namespace. – timgeb May 25 '22 at 11:42
  • I think Scenario 2 is what happens (except `myfunc` is read first, when it is imported). However Python can be made to import bytecode modules rather than re-compiling each time - see https://stackoverflow.com/questions/9913193/is-it-possible-to-import-a-compiled-python-file#:~:text=In%20a%20nutshell%2C%20to%20import,import%20module%20will%20work%20seamlessly. and https://peps.python.org/pep-3147/#flow-chart. – Stuart May 25 '22 at 11:42

1 Answers1

1

Unless you have the following at the end of your .py files (replacing method_name with the method you're creating):

if __name__ == '__main__':
    method_name()

what will likely happen is when you import the module, the imported module will run first followed by the code under your import call. In your example, you'd see myfunc run then lines 2 and 3 of app.py. It would print:

inside my dependency
inside app
inside dependency

The code at the top essentially checks a special variable of the code you're running to see if the interpreter should run the code. It's to avoid running the code on import unless you explicitly call the code within your app.

When you delete dependency, anything trying to use that now deleted module will throw an error because it's getting confused because it can't find it. So if you un-delete it, that ModuleNotFoundError will go away.

fmarz10
  • 95
  • 7
  • Hi fmarz10, I understood what you are saying. But tell me one thing- During compilation of app.py, when the compiler(interpreter) encounters the myfunc() then does it insert the code inside of myfunc() into app.py? Or does it insert an instruction that will later(during execution of app.pyc) instruct the interpreter to again open dependency.py and read the definition of myfunc() ? – Rambo May 25 '22 at 15:44
  • Sort of closer to the latter. Think of it more like you're borrowing a function from another location or file. The interpreter will read it in and if it gets called at any point in your code, then the borrowed function is run. So upon you importing, you're only instructing Python that there is some additional code you may use but it doesn't get run unless you call it (assuming you have the ```if __name__ == '__main__': method_name()```). – fmarz10 May 25 '22 at 16:58