Skip to content

AttributeError: 'utils' has no attribute 'collect_vendors' due to absolute import conflict #5

@koerper

Description

@koerper

Bug description

When using PriceCollector.collect(), an AttributeError: module 'utils' has no attribute 'collect_vendors' can occur if the user's project also contains a file named utils.py in the root directory (i.e., the current working directory when running a script or notebook).

This happens because chemprice/chemprice.py (and potentially other internal modules) uses an absolute import import utils. This can cause Python to import the user's utils.py instead of the chemprice package's own chemprice/utils.py file, which contains the collect_vendors function.

To Reproduce

Steps to reproduce the behavior:

  1. Create a Python project.
  2. Install chemprice.
  3. Create a file named utils.py in the root of the project (it can be empty or contain any code).
  4. In a script or notebook in the project root, try to use PriceCollector().collect(["CCO"]).
  5. See error: AttributeError: module 'utils' has no attribute 'collect_vendors'

Expected behavior

chemprice should always use its internal utils.py module, regardless of whether a utils.py file exists in the user's project directory.

Suggested Solution

Modify the import statement in chemprice/chemprice.py (and any other internal modules that import utils) from:
import utils
to a relative import:
from . import utils

This will ensure that the chemprice package always loads its own utils module.

Environment (example):

  • Python version: 3.11
  • chemprice version: 1.1.0
  • OS: macOS

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions