0

when I import a python file (let's call it x.py) to another file (let's call it y.py) will the computer run x.py the moment I import it to y.py?

so that if a code is originally only called once in x.py, will now run twice because y.py import it? if it is true, how to avoid this to happen?

  • What happened when you tried putting `how does import in python work?` into a search engine? When [I tried it](https://duckduckgo.com/?q=how+does+import+in+python+work%3F), the [first result](https://docs.python.org/3/reference/import.html) I got was the official documentation. The [second result](https://stackoverflow.com/questions/10501724/how-does-python-importing-exactly-work) I got was the linked duplicate. It's even *specifically* about your "will it run twice" question. There are further links that give you more information. Please read [ask]; you are expected to do basic research. – Karl Knechtel Apr 05 '21 at 03:26
  • "how to avoid this to happen" Put the code in a function and call it once. – GG. Apr 05 '21 at 03:26

0 Answers0