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?

oberprah
  • 157
  • 1
  • 8

1 Answers1

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.

Jalakoo
  • 3,523
  • 2
  • 21
  • 20
oberprah
  • 157
  • 1
  • 8
  • 1
    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). – hoefling May 26 '20 at 18:11
  • Thanks @hoefling. I'm new to python, are there better layouts than the `src`-layout or is it fine to use the `src`-layout? – oberprah May 27 '20 at 06:19
  • 1
    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. – hoefling May 27 '20 at 07:23