Python Import From Module
Import From Module
You can choose to import only parts from a module, by using the from
keyword.
Example
The module named mymodule
has one function
and one dictionary:
def greeting(name):
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
print("Hello, " + name)
person1 = {
"name": "John",
"age": 36,
"country": "Norway"
}
Example
Import only the person1 dictionary from the module:
from mymodule import person1
print (person1["age"])
Try it Yourself »
print (person1["age"])
Note: When importing using the from
keyword, do not use the module name when referring to elements in the module.
Example: person1["age"]
, not
(削除) mymodule.person1["age"] (削除ここまで)
Related Pages
Python Modules Tutorial Create a Module Variables in Modules Renaming a Module Built-in Modules Using the dir() Function