1

I have the following project structure

- src
  - main.py
  - foo
    - ...
- test
  - test_foo
    - ...
- Pipfile
- setup.cfg

If I run pipenv run pytest test I get import errors, because the test folder is outside the src folder and thus I cannot import the files correctly.

Is there a way to mark the src folder as my "source"-folder?

0

1 Answer 1

2

I have found a solution, but I do not know if there is a better one.

I've added a setup.py file, where I defined, that my src folder should be my "source"-folder.

from setuptools import find_packages, setup

setup(
    name="data_generation",
    package_dir={'': 'src'},
    packages=find_packages(where='src'),
)

To install everything I've run pipenv install -e . --dev. This is only needed for the first time, afterwards the Pipfile gets updated, and we can run pipenv install --dev.

Now we can run pipenv run pytest test and do not get any import errors anymore.

Sign up to request clarification or add additional context in comments.

3 Comments

This is clearly the only solution when using the src layout (you are testing against the installed code and not against your source code repository).
Thanks @hoefling. I'm new to python, are there better layouts than the src-layout or is it fine to use the src-layout?
It's a matter of choice. I always prefer the src layout because it makes testing installed packages easy, but if you're not planning to release a library or an application to the world, it may also be an overkill.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.