Bootstrap FreeKB - Python (Scripting) - Calling functions from another Python script
Python (Scripting) - Calling functions from another Python script

Updated:   |  Python (Scripting) articles

Often, shared functions are created in a "master" Python script, so that "child" Python scripts can use the shared functions.

Let's say functions.py contains the following.

#!/usr/bin/python
def Hello(greeting):
  return greeting

 

Here is how you could call functions.py from another Python script, such as testing.py. In this example, every function in functions.py will be made available in the testing.py script.

#!/usr/bin/python
from functions import *

result = Hello("Hello World")
print(result)

 

Or, if you want to limit the scope of the functions being imported, you can import specific functions.

#!/usr/bin/python
from functions import Hello

result = Hello("Hello World")
print(result)



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter fbb4e1 in the box below so that we can be sure you are a human.