Hello,
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:
![Place 2 scripts in a folder](https://datatofish.com/wp-content/uploads/2019/12/000_python_to_python.png)
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:
import python_2
print ('what are you up to?')
Where:
- 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?’
![How to Run One Python Script From Another](https://datatofish.com/wp-content/uploads/2019/12/001a_python_to_python.png)
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.
![Print in Python](https://datatofish.com/wp-content/uploads/2019/12/001b_python_to_python.png)
Note that you must first save the syntax that was captured in the python_2 script before calling it from another script:
![Save script](https://datatofish.com/wp-content/uploads/2019/12/018_python_to_python.png)
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:
![How to Run One Python Script From Another](https://datatofish.com/wp-content/uploads/2019/12/016_python_to_python.png)
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:
hello world
what are you up to?
Hope it helps!!