** * This article is from Udemy "[Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style](https://www.udemy.com/course/python-beginner/" Introduction to Python3 taught by active Silicon Valley engineers + application + American Silicon Valley style code style ")" It is a class notebook for myself after taking the course of. It is open to the public with permission from the instructor Jun Sakai. ** **
◆import Prepare directories and files like this.
lesson_package
├ __init__.py
└ utils.py
lesson.py
utils.py
def say_twice(word):
return (word + '!') * 2
lesson.py
import lesson_package.utils
r = lesson_package.utils.say_twice('hello')
print(r)
result
hello!hello!
You can load functions in other files by writing the full path like ʻimport lesson_package.utils`.
import lesson_package.utils
Is
from lesson_package import utils
You can write it like this. In that case,
lesson.py
from lesson_package import utils
r = utils.say_twice('hello')
print(r)
It can be used like this.
◆as
lesson.py
from lesson_package import utils as ut
r = ut.say_twice('hello')
print(r)
By specifying a character string with as, you can call it with your favorite character string.
Recommended Posts