Skip to content

Instantly share code, notes, and snippets.

@ldong
Created July 15, 2014 19:23
Show Gist options
  • Select an option

  • Save ldong/157e94be21623a1b376f to your computer and use it in GitHub Desktop.

Select an option

Save ldong/157e94be21623a1b376f to your computer and use it in GitHub Desktop.
python import module

Python import

Notation

Python import uses Package(dot notation)

Ways of importing

  1. import X, is preferred
  2. from X import *
  3. from X import a, b, c
  4. X = __import__(X)

Package vs Module

Module: Another python source file

Package: A Python package is simply a directory of Python module(s). __init__.py must exist for declaring as package.

Notes:

Relative imports only work for packages, but when you import a.py you are running a module instead.

.
├── driver_pkg_import.py
pkg
├── __init__.py
├── dot_import.py
├── module.py

References

https://docs.python.org/2/tutorial/modules.html http://effbot.org/zone/import-confusion.html http://programmers.stackexchange.com/questions/111871/module-vs-package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment