as long as circular dependencies aren't introduced by doing so
importing modules
modules can be executed directly as programs or scripts
Python execution model
to help understand when code is evaluated and executed
make programs executable
using command line arguments to get basic config data into program
simplest way to run Python scripts from shell:
$ python /path/to/file/script.py
Platform-specific Modules
Windows: use msvcrt module
Linux and MacOS X: use tty, termios, and sys modules
"""keypress - A module for detecting a single keypress."""try:import msvcrt
defgetkey():"""Wait for a keypress and return a single character string."""return msvcrt.getch()except ImportError:import sys
import tty
import termios
defgetkey():"""Wait for a keypress and return a single character string."""
fd = sys.stdin.fileno()
original_attributes = termios.tcgetattr(fd)try:
tty.setstraw(sys.stdin.fileno())
ch = sys.stdin.read(1)finally:#### NOTE: restores various terminal attributes after terminal has been put into raw mode in order to read single character
termios.tcsetattr(fd, termios.TCSADRAIN, original_attributes)return ch
# If either of the Unix-specific tty or termios are not found, allow the ImportError to propagate from here
Executable Directories
reader
|__ __main__.py <- This file will handle the execution
|__ reader
|__ __init__.py
|__ compressed
| |__ __init__.py
| |__ bzipped.py
| |__ gzipped.py
|__ reader.py
Modules as Singletons
# registry.py
_registry =[]defregister(name):
_registry.append(name)defregistered_names():returniter(_registry)# use_registry.pyimport registry
registry.register('my name')for name in registry.registered_names():print(name)