Home > Enterprise >  how to run python script/file multiple times
how to run python script/file multiple times

Time:09-29

I am having python file/script (main1.py) in that file/script I did operations like count word, replace words and change string etc. I want to make .exe file of that script/file (main1.py), and want to run that file/script (main1.py) 50 times, and In simple word if I run that .exe, my python file/script (main1.py) should run 50 times.

CodePudding user response:

I'm not sure what you want to accomplish or why. But executing a python script from another python script multiple times can be achieved in two ways.

1. Reimport

When you import a script it is executed.

import importlib
import main1 # first execution

for _ in range(49): # other 49 executions
    importlib.reload(main1)

2. Execute the script literally

I would not recommend this approach but that way as you spawn a new python interpreter for each script execution.

import subprocess

python_executable = "python3" # depends on how your python interpreter executable is named
for _ in range(50):
    subprocess.run(["python3", "main1.py"])

To the implicit second part of your question, how to create an exe out of a python script, I don't know the answer to.

  • Related