0

My proj structure is like this:

app/
  src
    resources
      __init__.py (contains foo method)
  tests
    resources
      __init__.py
    sometest.py

when I start sometest.py, as part of the imports, it eventually gets to this line located in src/core/some_module.py:

from resources import foo

when I run the test with pytest i get:

ImportError: cannot import name 'foo' from 'resources' (app/tests/resources/__init__.py)

If I explicitly change the import statement to:

from src.resources import foo

than it works, but for some reason ONLY when I run it via pycharm's UI, and if I try running:

poetry run pytest -ra --cov=src --cov-fail-under=80 --cov-config=.coveragerc

it fails with:

ModuleNotFoundError: No module named 'src'

can anyone explain what's happening here and how can I solve it so that the tests imports the foo that's inside /app/src ?

felisimo
  • 198
  • 14
  • 1
    what is happening : https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time ; how to solve : create a python package and [install it properly](https://packaging.python.org/en/latest/tutorials/packaging-projects/), then you can do the imports for your tests the "right" way. – Hoodlum Aug 11 '23 at 09:30
  • I appreciate the references and will read them later (it's a lot to read and understand), but for now, can you please explain in your own words? both resources dirs are already packages. what am i missing? – felisimo Aug 11 '23 at 09:37

1 Answers1

1

As mentioned by @Hoodlum in a comment:

[...] create a python package and install it properly

When you run a script in PyCharm the root of your project is added to PYTHONPATH, so Python will look if it can find packages there. You can find this option if you go to Run > Edit Configurations... and select one of the run configurations. You will find these two boxes are ticked enter image description here

The src folder is in your root folder, so that's why from src.resources import foo works, and from resources import foo doesn't.

When you run by command line your root is not added to PYTHONPATH, so nothing works. You can verify this by starting the Python REPL in the command prompt and listing all folders in sys.path, which is where Python looks for packages:

>>> import sys
>>> sys.path
[...]  # Your project folder is not here

The solution is to package your code and then install it in your own virtual environment. Having the __init__.py files in the right place (as you have) is not enough unfortunately, you also need to add pyproject.toml file and fill it with the appropriate information. It's all explained here https://packaging.python.org/en/latest/tutorials/packaging-projects/

edd313
  • 1,109
  • 7
  • 20
  • thank you! 2 questions please. 1. why did "from resources import foo" not work? when the path /app was added? 2. what do I need to package? I already have __init__ in resources dirs. Is that not enough? – felisimo Aug 11 '23 at 09:46
  • See the info I added – edd313 Aug 11 '23 at 13:23