I often hear a question, How do I run Python code simultaneously? In this article, I will show you how can you run two or multiple Python scripts or code files at the same time simultaneously.
Course for You: Learn Python in 100 days of coding
Create Sample Python code files
There are mainly two best methods to execute two or more Python files together. But before going to those methods let’s first create two Python files that we want to run at the same time.
I am creating two codes:
script2.py. My idea is to create two simple Python files to print the current system time. Now if you see the execution starting time for both of the codes, then only you can be sure those two Python files are running simultaneously or one after another.
By this code, I am just trying to print the current time (or when started execution) from
sleep() function to wait this code for 10 seconds.
import datetime from time import sleep current_time = datetime.datetime.now().strftime("%H:%M:%S") sleep(10) print("Time in Script1", current_time)
In this second Python script, we are doing the same thing. Made just one change in the printing string.
import datetime from time import sleep current_time = datetime.datetime.now().strftime("%H:%M:%S") sleep(10) print("Time in Script2", current_time)
Once you are done with those two codes, save them inside a folder (your working directory). In my case, I saved those two codes inside this folder:
Method 1: Using Command Line
To start this command line method, first open a command prompt in Windows or a terminal in Linux or Mac. Then go to that directory by using
Now type the below command to run those two Python files at the same time.
python script1.py & python script2.py
In the above output, you can see that the above command is not executing those two Python files at the same time. You can see the difference in seconds. I kept each code 10 seconds sleep.
script1.py started at
19:50:26 on the other hand
script2.py started at
19:50:37. This 10-second gap is for 10-second sleep time in the code. That means those Python codes are running one after another (not simultaneously).
This command is useful to run multiple python scripts but not simultaneously. Another disadvantage of this command is that, both of those Python code files need to be inside same folder (in my case
If you really want to start multiple Python scripts at once you need to open two terminals and execute those two codes separately. For example, this time I kept
script1.py inside C drive (
script2.py inside the same D drive (
python script1.py inside C:\ drive terminal and
python script2.py from D:\ drive terminal like below.
As you can see in the above output screen, I ran those two Python scripts from two different directories in two different terminals (command prompt), now those are running simultaneously. You can check the execution time (second) for those two scripts.
Method 2: Using Python Script
You can also run multiple Python scripts simultaneously using a Python script itself. This approach allows you to have more control and flexibility over the execution of your scripts.
# run multiple Python codes simultaneously using a Python script import subprocess # Define the Python scripts you want to run scripts = ["C:/Python/script1.py", "D:/Python/script2.py"] # Run the scripts concurrently processes =  for script in scripts: command = ["python", script] processes.append(subprocess.Popen(command)) # Wait for all processes to finish for process in processes: process.wait()
As you can see in the above code, using this technique you can run two or multiple Python codes from any different directories. In this script, we are using the subprocess module to run your Python scripts concurrently.
I saved this code as
run_two_scripts.py. You can see output in the below terminal screenshot.
As you can see in the above terminal output, both codes are running and working together exactly at the same time. I believe this is the best approach if you want to start or run multiple Python files simultaneously just by one click.
So can we run multiple Python scripts at once? Yes in this article I have mentioned two different methods to run multiple Python scripts simultaneously. The first one is using command prompt and the second one using a Python script itself.
Some frequently asked questions I missed to answer in the above article. Let me cover those in this FAQ part.
Can a Python script run another Python script?
Yes, one Python script can execute another Python script. You just need to import and link those two Python scripts. In the second method, I showed you, how to run and execute two Python scripts simultaneously using the subprocess module in Python.
Can I run 2 Python scripts at the same time in Pycharm?
Yes, you can run two Python scripts simultaneously in PyCharm:
- Open PyCharm and load your project.
- Create two Run Configurations for your scripts.
- Run them concurrently or even run the same configuration multiple times if it has “Allow parallel run” enabled.
But I will not recommend you to run multiple scripts simultaneously in Pycharm because it is a development tool not a runtime environment.
How to run 2 Python scripts at the same time in Visual Studio Code?
You can run two Python scripts simultaneously in VS Code using the integrated terminal:
- Open VS Code and navigate to your script folder.
- Open the integrated terminal.
cdto navigate to the script directory.
- Run your scripts with
python script1.py &and
python script2.py &.
This is it for this article. If you have any questions or suggestions regarding this article, please please drop a comment below. If you want to learn Python quickly then this Udemy course is for you: Learn Python in 100 days of coding.
Hi there, I’m Anindya Naskar, Data Science Engineer. I created this website to show you what I believe is the best possible way to get your start in the field of Data Science.