0

How can I write a Python program that runs all Python scripts in the current folder? The program should run in Linux, Windows and any other OS in which python is installed.

Here is what I tried:

import glob, importlib

for file in glob.iglob("*.py"):
    importlib.import_module(file)

This returns an error: ModuleNotFoundError: No module named 'agents.py'; 'agents' is not a package (here agents.py is one of the files in the folder; it is indeed not a package and not intended to be a package - it is just a script).

If I change the last line to:

    importlib.import_module(file.replace(".py",""))

then I get no error, but also the scripts do not run.

Another attempt:

import glob, os

for file in glob.iglob("*.py"):
    os.system(file)

This does not work on Windows - it tries to open each file in Notepad.

Erel Segal-Halevi
  • 33,955
  • 36
  • 114
  • 183

3 Answers3

0

You need to specify that you are running the script through the command line. To do this you need to add python3 plus the name of the file that you are running. The following code should work

import os
import glob

for file in glob.iglob("*.py"):
    os.system("python3 " + file)

If you are using a version other than python3, just change the argument from python3 to python

Thomas Hayes
  • 102
  • 6
0

Maybe you can make use of the subprocess module; this question shows a few options.

Your code could look like this:

import os
import subprocess

base_path = os.getcwd()
print('base_path', base_path)

# TODO: this might need to be 'python3' in some cases
python_executable = 'python'
print('python_executable', python_executable)

py_file_list = []
for dir_path, _, file_name_list in os.walk(base_path):
    for file_name in file_name_list:
        if file_name.endswith('.csv'):
            # add full path, not just file_name
            py_file_list.append(
                os.path.join(dir_path, file_name))

print('PY files that were found:')
for i, file_path in enumerate(py_file_list):
    print('   {:3d} {}'.format(i, file_path))

    # call script
    subprocess.run([python_executable, file_path])

Does that work for you?


Note that the docs for os.system() even suggest using subprocess instead:

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function.

Ralf
  • 16,086
  • 4
  • 44
  • 68
0

If you have control over the content of the scripts, perhaps you might consider using a plugin technique, this would bring the problem more into the Python domain and thus makes it less platform dependent. Take a look at pyPlugin as an example.

This way you could run each "plugin" from within the original process, or using the Python::multiprocessing library you could still seamlessly use sub-processes.

SourceSimian
  • 1,301
  • 8
  • 5