0

I have a Python script named Build_all.py and in this Build_all.py I called several other scripts named First_build.py, Second_build.py, etc to run. Inside each First/Second_build.py I want to have a variable to automatically save the script name that is called. How should I do it?

In other words, how to get the name of the python script that is called - not the python file which is currently executing.

I search online and find that sys.argv[0] and inspect.getfile(inspect.currentframe()) and __file__. But all of these three methods give me the current executing script name (Build_all.py) not what I want (First_build.py)

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Does this answer your question? [what does the \_\_file\_\_ variable mean/do?](https://stackoverflow.com/questions/9271464/what-does-the-file-variable-mean-do) – Aaron Jul 11 '23 at 21:13
  • 3
    `__file__` shouldn't give `Build_all.py` from inside `First_build.py`. How are you using it? Make a [mre]. BTW, welcome to Stack Overflow! Check out the [tour], and [ask] if you want more tips. – wjandrea Jul 11 '23 at 21:16
  • How are you calling the other scripts? As subproceses or with `import`? – Barmar Jul 11 '23 at 21:22
  • if you call `__name__` from within First_build.py and import First_build into Build_all, you should get First_build – srn Jul 11 '23 at 21:25
  • @Barmar I am using exec(open("..xxx.py").read()) – Vincent Jul 13 '23 at 17:27
  • That won't set `__file__` to `xxx.py`, if that's what you want. The string that you're executing has no recollection of the file it was read from. – Barmar Jul 14 '23 at 02:38

0 Answers0