【python】ファイル分割をする
別ファイルをモジュールとし、インポートして利用する
test1.py
code:test1.py
# test1.py
def greet(name):
return f"Hello, {name}!"
test2.py
code:test2.py
# test2.py
def connect_to_db():
print("Database connected")
main.py
code:main.py
# main.py
from test1 import greet
from test2 import connect_to_db
def main():
print(greet("Alice"))
connect_to_db()
if __name__ == "__main__":
main()
実行
code:run
$ python main.py
Hello, Alice!
Database connected