The http://docs.python.org/2/whatsnew/2.5.html#pep-328-absolute-and-relative-imports:
Absolute and Relative Imports explans very detailed.
The absolute_import feature is default in Python 3.x. (I use Python 2.7.x)
Use the examples:
pkg
├── __init__.py
├── main.py
└── string.py
The content of string.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def say_hello():
print "say hello"
The content of first version main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import string
string.say_hello()
# move to the parent dir of pkg
$ python -m pkg.main
say hello
This will use the relative string.py module, not the Python's standard string module.
When use absolute_import:
From Python manual:
Once absolute imports are the default, import string will always find
the standard library’s version. It’s suggested that users should begin
using absolute imports as much as possible, so it’s preferable to
begin writing from pkg import string in your code.
from __future__ import absolute_import
#import string # This is error because `import string` will use the standard string module
from pkg import string
string.say_hello()
Relative imports are still possible by adding a leading period to the
module name when using the from ... import form:
from __future__ import absolute_import
from . import string # This is the same as `from pkg import string`
string.say_hello()
or
from __future__ import absolute_import
from .string import say_hello
say_hello()
Use print(string) to see which string module to import
main.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import absolute_import
import string
print(string)
string.say_hello()
If run code by:
cd pkg
$ python pkg/main.py
<module 'string' from '/path/to/my/pkg/string.pyc'>
say hello
It will always use local string.py, because current path is the first in sys.path
change main.py to:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from . import string
print(string)
string.say_hello()
run code:
cd pkg
$ python pkg/main.py
Traceback (most recent call last):
File "pkg/main.py", line 3, in <module>
from . import string
ValueError: Attempted relative import in non-package
This answer is detailed:
https://stackoverflow.com/a/11537218/1276501
To elaborate on @Ignacio's answer: the python import mechanism works
relative to the name of the current file. When you execute a file
directly, it doesn't have it's usual name, but has "main" as its name
instead. So relative imports don't work. You can, as Igancio
suggested, execute it using the -m option. If you have a part of your
package that is mean to be run as a script, you can also use the
package attribute to tell that file what name it's supposed to have in
the package hierarchy. See http://www.python.org/dev/peps/pep-0366/
for details.
Absolute/Relative import is to package.
In Python 2.x(by now is Python 2.7.x), the default import feature is implicit relative import.
As above see, it will first import the same-named module under package. Use absolute import as default, Python will only import by the sys.path sequence.
And if you want to use relative import, you must use explicit relative import
That as list in import this:
Explicit is better than implicit
References: