- define my_module.py
def hello():
print("hello world!\n")
- import my_module
>>> import my_module
>>> my_module.hello()
hello world!
>>> my_module.__name__
'my_module'
- from my_module import *
>>> from my_module import *
>>> hello()
hello world!
- run module
from my_module import *
if __name__ == "__main__":
import sys
hello(sys.argv[1])
def hello(name="world!"):
print("hello " + name + "!")
- sys.path
>>> import sys
>>> sys.path
dir(): lists the names you have defined currently
>>> import sys
>>> from my_module import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'hello', 'sys']
- package:
path/package organize:
first/
__init__.py __init__.pyc second
hello.py hello.pyc __init__.py __init__.pyc
import package module
import first.second.hello
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'first', 'sys']
>>> first.second.hello.hello()
hello
- __init__.py
__all__ = ["hello"]
use to import all
from first.second import *