Suppose we have a code file that we run as a script. The file grows, and functions and other definitions appear.
At some point, we want to reuse a function from this module in another module. So you have to import. In this lesson, we'll understand how importing scripts works.
Importing scripts
Let's simulate the situation described above. It is what the original script will look like:
# file <first_script.py>
def greet(who):
print(f'Hello, {who}!')
greet('Bob')
greet('Ann')
Now let's look at the new script in which we want to reuse the greet
function from the first script:
# file <second_script.py>
from first_script import greet
greet('Thomas')
Let's run the first script and then the second script. Both files are in the current directory:
python3 first_script.py
Hello, Bob!
Hello, Ann!
python3 second_script.py
Hello, Bob!