1

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 bar instead of import foo.bar, given that I only have 1 module
  • If so, is it best practice to put bar.py directly in the src folder
  • If so, should I configure tox differently?
asked May 9, 2024 at 9:25

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.