A module is a .py file other code can import. In this editor, write it to /uploads (cwd after files exist), then import the name without .py.
Goal
Write kiosk.py, import it, and call line_total.
Write, then import
from pathlib import Path
Path("kiosk.py").write_text(
'''TAX = 0.16
def line_total(units, price):
return round(units * price * (1 + TAX), 2)
''',
encoding="utf-8",
)
import kiosk
print(kiosk.line_total(2, 10))
print("file:", kiosk.__file__)kiosk.__file__ should live under /uploads.
Import a name
from pathlib import Path
Path("kiosk.py").write_text(
'''def city_label(city):
return str(city or "").strip().title()
''',
encoding="utf-8",
)
from kiosk import city_label
print(city_label(" nairobi "))Banner file
Download kiosk.py from this lesson, Add files, then:
import kiosk
print(kiosk.line_total(2, 10))
print(kiosk.city_label("kisumu"))Pitfall
import kiosk.py is invalid. The module name is kiosk. If you change the file, this editor drops /uploads modules at the start of each Run so you see the new text.