Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Export import

  • all

  • import

  • from

  • from mod import a,b,_c - import 'a', 'b', and '_c' from 'mod'

  • from mod import * - import every name listed in all of 'mod' if all is available.

  • from mod import * - import every name that does NOT start with _ (if all is not available)

  • import mod - import 'mod' and make every name in 'mod' accessible as 'mod.a', and 'mod._c'

def a():
    return "in a"

b = "value of b"

def _c():
    return "in _c"

def d():
    return "in d"
from my_module import a,b,_c

print(a())     # in a
print(b)       # value of b
print(_c())    # in _c

print(d())
# Traceback (most recent call last):
#   File ".../examples/modules/x.py", line 7, in <module>
#     print(d())
# NameError: name 'd' is not defined
from my_module import *

print(a())     # in a
print(b)       # value of b

print(d())     # in d


print(_c())

# Traceback (most recent call last):
#   File ".../examples/modules/y.py", line 9, in <module>
#     print(_c())    # in _c
# NameError: name '_c' is not defined