Search code examples
pythonpython-2.7importpython-2.ximporterror

Local import failing with Python 2.7 only


I am running a test suite with tox, and one of my local imports is performing fine in Python 3.7 but failing with Python 2.7.

I get the error:

ImportError while importing test module /home/myname/tests/myfolder/test_triad_2020_orgs.py
Traceback:
tests/myfolder/test_triad_2020_orgs.py:5: in <module>
from [testssibbling].myfolder.triad.triad_2020_orgs import Orgs

Import Error: No module named triad.triad_2020_orgs

Any ideas on why this could be? The file definitely exists. Is there a problem with the file name for 2.7 conventions?


Solution

  • In order to import python modules in python 3.2 and ealier, there should be a file named __init__.py which is responsible to introduce the package as a python package. From python 3.3+ it is implicitly executed.

    Based on this documentation we have two types of packages: regular package (contain __init__.py) and namespace package (not contained). You need to add this file to import the file in python 3.2 and ealier.

    You may take a look at this answer to find more information about this file.