python - Imports in files inside module give ImportError in python2 & works fine for python3 -


if have file 'needy' in module 'module', trying access 'utils' in same 'module'. inside 'needy':

from module.utils import fly    # works in python3 

whereas

from utils import fly           # works in python2 

example

python/pulla git:(master) ✗ ± pulla traceback (most recent call last):   file "/usr/local/bin/pulla", line 9, in <module>     load_entry_point('pulla==0.0.5', 'console_scripts', 'pulla')()   file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/pkg_resources.py", line 357, in load_entry_point     return get_distribution(dist).load_entry_point(group, name)   file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/pkg_resources.py", line 2394, in load_entry_point     return ep.load()   file "/system/library/frameworks/python.framework/versions/2.7/extras/lib/python/pkg_resources.py", line 2108, in load     entry = __import__(self.module_name, globals(),globals(), ['__name__'])   file "/library/python/2.7/site-packages/pulla/main.py", line 9, in <module>     pulla.pulla import pulla   file "/library/python/2.7/site-packages/pulla/pulla.py", line 5, in <module>     pulla.utils import is_this_a_git_dir importerror: no module named utils 

the import works fine when using python3 executable.

this works fine(similar python3) command line in python2.

python/pulla git:(master) ✗ ± python2.7 python 2.7.6 (default, sep  9 2014, 15:04:36)  [gcc 4.2.1 compatible apple llvm 6.0 (clang-600.0.39)] on darwin type "help", "copyright", "credits" or "license" more information. >>> import pulla >>> import pulla.utils >>> pulla.utils import is_this_a_git_dir >>> is_this_a_git_dir('.') true 

the code particular import here on github.

this sounds implicit relative import. if module has

from __future__ import absolute_import 

at top, force import interpreted absolute import, it's looking utils module on sys.path, , not finding it. importerror.

hmm, can't see in code. anyway, if still want import relative one, can replace with

from .utils import fly 

this explicit package-relative import.

lots of information absolute vs relative imports.