I am trying to run tox on a module.
Consider the following python package:
foo
├── tox.ini
├── setup.cfg
│ tests
│ └── test_bar.py
└── src
├── data
│ └── data.csv
├── __init__.py
└── bar.py
Where bar.py has:
import importlib.resources
def get_data_file(file_name):
return importlib.resources.files("data").joinpath(file_name).read_text()
and test_bar.py has:
import bar
def test_bar():
data = bar.get_data_file('data.csv')
assert True
I can use this package as follows:
import bar
data = bar.get_data_file('data.csv')
Moreover, pytest passes.
However, tox returns the following error message in tests/test_bar.py
ModuleNotFoundError: No module named 'bar'
Initial approach
Initially, the package had the following structure:
foo
├── tox.ini
├── setup.cfg
│ tests
│ └── test_bar.py
└── src
└── foo
├── data
│ └── data.csv
├── __init__.py
└── bar.py
This structure is in accordance to PyScaffold, and works with tox. However, since this package only has one module, I was hoping to avoid having to import foo.bar, and instead import bar.
I tried the suggestion from https://github.com/pyscaffold/pyscaffold/discussions/399 i.e. add from .foo import bar or from . import bar in __init__.py.
However, if I then try to import bar, I get ModuleNotFoundError: No module named 'bar'.
Moreover, my understanding is that anything in the src folder is considered to be part of the package. Therefore I figured I should be able to put bar.py directly in src.
In conclusion, I believe to have the following questions:
- Is it best practice to try to aim for
import barinstead ofimport foo.bar, given that I only have 1 module - If so, is it best practice to put
bar.pydirectly in thesrcfolder - If so, should I configure
toxdifferently?