2

Even for as long as I've worked in Python, I still occasionally run into issues with my package structure.

I'm trying to run $ pipenv run pytest from the root of the directory structure below (i.e. .. above src/)

src/
   physics/
      __init__.py
      pathing.py
   road/
      tests/
          __init__.py
          test_traffic.py
      __init__.py
      traffic.py
   __init__.py
   main.py

traffic.py:


    from physics import pathing

    class Intersection():
        ...

    class Vehicle():
        ...

test_traffic.py


    from src.road.traffic import Intersection, Vehicle

    def test_intersection():
        ...

However, I get hit with:

======================================================================= ERRORS ========================================================================
___________________________________________________ ERROR collecting src/road/tests/test_traffic.py ___________________________________________________
ImportError while importing test module '/Users/justian/scripts/py-traffic-sim/src/road/tests/test_traffic.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
src/road/tests/test_traffic.py:1: in <module>
    from src.road.traffic import Intersection, Vehicle
src/road/traffic.py:13: in <module>
    from physics import pathing
E   ModuleNotFoundError: No module named 'physics'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
================================================================== 1 error in 0.38s ===================================================================

It looks like I'm misunderstanding how pytest is importing my modules or misunderstanding how my modules are referencing one another. Both $ pipenv run pytest and $ pipenv run python -m pytest produce the same result.

How can I properly import Intersection and Vehicle into test_traffic.py?

Justian Meyer
  • 3,623
  • 9
  • 35
  • 53

1 Answers1

5

Remove the __init__.py file from src.

e.g.

src/
   physics/
      __init__.py
      pathing.py
   road/
      tests/
          __init__.py
          test_traffic.py
      __init__.py
      traffic.py
   __init__.py    # Remove this one
   main.py
Dipen Dadhaniya
  • 4,550
  • 2
  • 16
  • 24
  • So this works for testing if I remove `src/__init__.py` and change the import in test_traffic.py to `from road.traffic import Intersection, Vehicle`, but how then do I run my project with pipenv? `$ pipenv run src/main.py` produces: `Error: the command src/main.py could not be found within PATH or Pipfile's [scripts].` – Justian Meyer Jan 21 '20 at 05:40
  • Note there are also other files in src/ other than main.py that I didn't list prior to better scope the question. – Justian Meyer Jan 21 '20 at 05:42
  • Sorry, I mistyped the command in my terminal. I believe `pipenv run python src/main.py` works. Appreciate the help! – Justian Meyer Jan 21 '20 at 05:44