In this lesson, you’ll learn about executing a module as a script. Any .py
file that contains a module is essentially also a Python script, and there isn’t any reason it can’t be executed like one.
Here’s mod.py
, as it was defined earlier:
s = "Computers are useless. They can only give you answers."
a = [100, 200, 300]
def printy(arg):
print(f'arg = {arg}')
class Classy:
pass
This can be run as a script:
$ python3 mod.py
There are no errors, so it apparently worked. Granted, it’s not very interesting. As it is written, it only defines objects. It doesn’t do anything with them, and it doesn’t generate any output.
theramstoss on June 14, 2020
How do you set it up so that you don’t have to import sys and append the path every time? Thanks.