Local tests passing while circleci brings up module import errors for local file

When I run pytest locally, everything passes. I just added CircleCI to see what I can do with it, and pushed it to GitHub, but I keep getting a ModuleNotFoundError even though the module in question is a local file.

This is the way my files are set up:

folder
  thing
    blah.py
  tests/thing
    test_blah.py

In tests/thing, I run:

sys.path.append("/Users/user/folder/")
from thing/blah import Class

And then I get this error:

tests/thing/test_blah.py:8: in <module>
    from thing.blah import Class
E   ModuleNotFoundError: No module named 'thing'

I’m not entirely sure what’s going on. Can someone help?

I am using the basic config file given when you start a CircleCI project.

version: 2.1
orbs:
  python: circleci/python@2.1.1
jobs:
  build-and-test: 
    docker:
      - image: cimg/python:3.11.2
    steps:
      - checkout
      - python/install-packages:
          pkg-manager: pip
      - run:
          name: Run tests
          command: pytest
workflows:
  test_app:
    jobs:
      - build-and-test
1 Like

I am guessing that you also have a requirements.txt file somewhere. Does it list pytest as a package?

There is a python ORB example for working with pip at

This has pytest being installed as an independent step to allow control over the cache settings.

Also, what do you have in tests/thing/test_blah.py as this is where the python error is thrown when one of your import commands is executed.