I have a script called test.py
, with the following code (I have simplified things significantly):
from foo import Bar
bar = Bar()
result = bar.do_something()
But I don't just have a single script called foo
. I have many scripts called foo
, organised in the following directory structure:
└── project
├── code
│ ├── test.py
└── scripts
├── script_1
└── foo.py
├── script_2
└── foo.py
├── script_3
└── foo.py
├── script_4
└── foo.py
├── script_5
└── foo.py
Each foo.py
has a slightly different implementation of something. And what I want to do with test.py
, is to test all the scripts out, by importing each one and running some tests on it. Below is some code for this (*'s indicate pseudo-code)
*Get all script directories*
*For each directory in script directories:*
*import foo.py from this directory*
bar = Bar()
result = bar.do_something()
*Save the result for this directory*
How can I do this? In particular, how can I iteratively import scripts, e.g.,
*import foo.py from this directory*
?