Skip to content

Instantly share code, notes, and snippets.

@photonxp
Last active September 25, 2016 13:17
Show Gist options
  • Save photonxp/be9f7ca555543dd8608c to your computer and use it in GitHub Desktop.
Save photonxp/be9f7ca555543dd8608c to your computer and use it in GitHub Desktop.
anypackage/
__init__.py
xxx.py # class WWW is originally defined in this xxx file.
generally speaking, python can only import class from modules, which is usually a .py file.
import WWW from xxx
But, we can also import class from a package(folder) directly, rather than from the module. This is where an __init__.py file can step onto the stage.
anypackage/
__init__.py # class WWW is then moved here.
xxx.py
After we move the class from ordinary python file xxx.py to special python file __init__.py, we can import it from "anypackage" directly:
import WWW from anypackage
Why do we need to import from package, if we can import from module?
When we import from a package, it can probably be viewed as a shortcut, as soft link in linux system
http://stackoverflow.com/questions/582723/how-to-import-classes-defined-in-init-py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment