To call one python script from another Python Script
Step 1: Place the Python Scripts in the Same Folder
To start, you’ll need to place your Python scripts in the same folder.
For example, I placed two Python scripts (called python_1 and python_2) in the same folder as below:
The ultimate goal is to run the python_2 script from the python_1 script.
Step 2: Add the Syntax
Next, add the syntax to each of your scripts.
For instance, I added the following syntax under the python_1 script:
print ('what are you up to?')
- The first line of ‘import python_2’ in the python_1 script, would call the second python_2 script. Whenever you want to run one Python script from another, you’ll need to import the exact name of the Python script that you’d like to call
- The second part of the code simply prints the expression of ‘what are you up to?’
Now let’s add the syntax under the python_2 script:
print ('hello world')
In this case, the expression of ‘hello world’ would be printed when running the second script.
Note that you must first save the syntax that was captured in the python_2 script before calling it from another script:
Step 3: Run One Python Script From Another
Now you’ll need to run the script from the python_1 box in order to call the second script:
Notice that the results of the python_2 script would be displayed first, and only then the results of the python_1 script would be displayed:
what are you up to?
Hope it helps!!