Skip to content

Notes on python imports

Anubhab Chakraborty edited this page Nov 26, 2021 · 1 revision

Documenting issues faced with python imports.

System specifications

OS: Windows 10
Editor: VSCode with Python extension by Microsoft
Terminal: powershell

python: Python 3.9.7
> echo %PYTHONPATH%
C:\Users\chakr\projects;C:\Users\chakr\projects\pyamiimage;C:\Users\chakr\projects\pyami;

Note that the projects directory, pyamiimage directory and the pyami directory are all in PYTHONPATH. (See the directory structure below)

Directory structure

projects/
  |_pyamiimage/
    |- __init__.py
    |_ pyimage/
      |- __init__.py
      |- octree.py
      |- image_lib.py
      |- tesseract_hocr.py
      |- examples_tesseract.py
      |...
    |_ test/
      |- __init__.py
      |- test_octree.py
      |- test_image_lib.py
      |- test_tesseract_hocr.py
      |...
    |...
  |_pyami
  |...

The project name is pyamiimage and under the project there are several subdirectories.
Couple of subdirectories we are interested in are:

  • pyimage: contains the source files of pyamiimage.
  • test: contains all the python tests.

Note that every directory contains an __init__.py file.

Problem

In test_octree.py we had the import statement:

from pyimage import octree
from pyimage.image_lib import Quantizer

This works fine, when testing test_octree.py within VSCode (or PyCharm) However it fails when running pytest test_octree.py from the terminal.
This problem is solved when we instead do relative imports with .. before the import path, like:

from ..pyimage import octree
from ..pyimage.image_lib import Quantizer

However, making that change everywhere, we run into other problems, where the relative imports seems to not be working, but the normal imports were working.

Temporary Solution

To solve this problem temporarily we've used try...except sequences for imports.

try:
  from ..pyimage import octree
  from ..pyimage.image_lib import Quantizer
except Exception as e:
  from pyimage import octree
  from pyimage.image_lib import Quantizer

A similar solution has been implemented in pyami

try:
    from ..py4ami.dict_lib import AMIDict, AMIDictError, Entry
    logging.info(f"loaded py4ami.dict_lib")
except Exception:
    try:
        from py4ami.dict_lib import AMIDict, AMIDictError, Entry
    except Exception as e:
        logging.severe(f"Cannot import from py4ami.dict_lib")