Skip to content

Commit 8f4de08

Browse files
browniebrokehramezanibluetech
authored
docs: configuration for the src layout (#989)
* docs: configuration for the src layout Expand the documentation to explain how to configure the pythonpath for projects using the src layout. Fix #738 * Fix incorrect formatting Co-authored-by: Hasan Ramezani <[email protected]> * Prefer the pytest 7 way Co-authored-by: Hasan Ramezani <[email protected]> Co-authored-by: Ran Benita <[email protected]>
1 parent 14b993d commit 8f4de08

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

docs/managing_python_path.rst

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,39 @@ add this directly to your project's requirements.txt file like this::
7575
pytest-django
7676

7777

78-
Using pytest-pythonpath
79-
~~~~~~~~~~~~~~~~~~~~~~~
78+
Using pytest's ``pythonpath`` option
79+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
80+
81+
You can explicitly add paths to the Python search path using pytest's
82+
:pytest-confval:`pythonpath` option.
83+
This option is available since pytest 7; for older versions you can use the
84+
`pytest-pythonpath <https://pypi.python.org/pypi/pytest-pythonpath>`_ plugin.
85+
86+
Example: project with src layout
87+
````````````````````````````````
88+
89+
For a Django package using the ``src`` layout, with test settings located in a
90+
``tests`` package at the top level::
91+
92+
myproj
93+
├── pytest.ini
94+
├── src
95+
│ └── myproj
96+
│ ├── __init__.py
97+
│ └── main.py
98+
└── tests
99+
├── testapp
100+
| ├── __init__.py
101+
| └── apps.py
102+
├── __init__.py
103+
├── settings.py
104+
└── test_main.py
105+
106+
You'll need to specify both the top level directory and ``src`` for things to work::
80107

81-
You can also use the `pytest-pythonpath
82-
<https://pypi.python.org/pypi/pytest-pythonpath>`_ plugin to explicitly add paths to
83-
the Python path.
108+
[pytest]
109+
DJANGO_SETTINGS_MODULE = tests.settings
110+
pythonpath = . src
111+
112+
If you don't specify ``.``, the settings module won't be found and
113+
you'll get an import error: ``ImportError: No module named 'tests'``.

0 commit comments

Comments
 (0)