0

I have a Main script in Spyder that calls several functions contained in 6 different scripts (.py). I had to this way because the scripts are also used in different projects.

Currently, I have to run each script (containing several functions each) separately by hand, which is tiring, by clicking in the "green triangle" before launching the Main Script so that the functions contained in each script are stored in the working environment.

My question is: Would it be possible to automatically run each script directly from the Main Script and not running one after the another by hand?

pedro_galher
  • 334
  • 8
  • 18
  • 1
    Yes, it is possible. There is the built-in module [runpy](https://docs.python.org/2.7/library/runpy.html). – Sven-Eric Krüger May 14 '19 at 14:31
  • I have just noticed there was a question asking more or the same: https://stackoverflow.com/questions/3657955/how-to-execute-another-python-script-from-your-script-and-be-able-to-debug – pedro_galher May 14 '19 at 14:54

2 Answers2

1

When you execute an import statement, the source file being imported is executed. So, for example, if you have thing.py and you execute import thing, all the code in thing.py will be run.

Also, as noted in a comment by Sven Krüger: you can use runpy.run_path, which I think is overall a better solution than my original suggestion.

gmds
  • 19,325
  • 4
  • 32
  • 58
  • Unfortunately, It doesn't work, even though I have added the path of the file where the script is to the Pythonpath manager – pedro_galher May 14 '19 at 14:32
1

Try

from filename import *

instead of

import filename

No .py extension in the import.

pdrersin
  • 311
  • 3
  • 10