Executing Commands in a Python File from other Python Files in the Same Directory

Executing Commands in a Python File from other Python Files in the Same Directory

Overview

This is a basic demonstration of the file "test1.py" which contains the Flask microframework and returns the output from another Python file called "test2.py". Both "test1.py" and "test2.py" have to be in the same directory.

Materials

  • Python

  • Flask

  • Command Prompt, Terminal or IDE

  • Make two Python files (both files must be in the same directory)

Procedure

Below is the "test1.py" file that runs Flask.

# test1.py
from flask import Flask   
import test2                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  

app = Flask(__name__)

@app.route("/")
def run_test2():                                                                                                                                                                                                                                                                                                                                                            
    return test2.run_a_function()

if __name__== '__main__':
    app.run(host='0.0.0.0', debug=True, port=5000)

This file imports the Flask module, defines the app route and sets the declared function within the route to return a statement locally at port 5000. This file also imports the file "test2.py". "test2.py" is in the same directory as "test1.py". In Python, it is possible to import other files and run them in different files. This is efficient for organizing long lines of code that fulfill one task to separate into one file and then the main file can read that file to run the code. As stated previously, "test1.py" is importing "test2.py" to run the content in "test2.py". When importing files, such as "test2.py", the ".py" can be excluded as shown at the top of the "test1.py" file.

As shown above, in the “def run_test2() function”, the statement returns the contents of “test2.py”. Returning content from another file can be done by declaring in the following format: “filename.functionname()”. As shown in “test1.py”, the return statement is “test2.run_a_function()” since test2 is the file name and the function within test2 is called “run_a_function()”.

Below is the "test2.py" file:

# test2.py
def run_a_function():
    return "Just test 2 running"

"test2.py" contains the function "run_a_function()" and returns the statement "Just test 2 running" if it ran successfully. When running "test1.py" in the same directory as "test2.py", the following will occur:

"test1.py" will return the statement written in "test2.py" since "test1.py" was configured to return the information in "test2.py".