Python imports and PYTHONPATH.
- “import foo.bar.x” style:
- Full name of “foo.bar.x” must be used to refer to what’s imported;
- Each item except for the last one (“x”) must be a package;
- The “x” can be a module or a package;
- The “x” can’t be any of class, function, or variable defined in 2nd last item.
- “from foo.bar import x” style
- “x” is now only directly available for use;
- “x” can be either a module or a subpackage;
- “x” can also be function, class, variable defined in the module.
- Package-relative import with . and .. using “from foo.bar import x” style:
- Environment variable like linux PATH;
- Defines search path for python interpreter to look for modules to import;
- NOTE that the following are always available in search path:
- Python installation site-packages directory;
- Current directory;
- Current module’s directory (relative import to be possible).
- Set PYTHONPATH: