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?

asked May 25, 2022 at 11:32
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. Commented May 25, 2022 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 stackoverflow.com/questions/9913193/…. and peps.python.org/pep-3147/#flow-chart. Commented May 25, 2022 at 11:42

1 Answer 1

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.

answered May 25, 2022 at 12:54
Sign up to request clarification or add additional context in comments.

2 Comments

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() ?
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()).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.