Does the contents of the original file run as they are when importing in Python?

Asked 2 years ago, Updated 2 years ago, 90 views

In Python 3 environment, when I tried to import a function of another file in the same folder, the import file was processed.
(print statement displayed first in the import file.)

I just want to take out the function, but if I do import, will it run as it was?

python3 import

2022-09-30 10:39

1 Answers

Yes.
Import will run the original file as it is.

If you do not want to run it during import and only want to extract functions, rewrite the .py file you are importing so that if__name__=="__main__": does not contain functions.

Reference:
Python import

that you can see in 10 minutes


2022-09-30 10:39

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.