Skip to content

Commit

Permalink
docs: configuration for the src layout (#989)
Browse files Browse the repository at this point in the history
* 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 <hasan.r67@gmail.com>

* Prefer the pytest 7 way

Co-authored-by: Hasan Ramezani <hasan.r67@gmail.com>
Co-authored-by: Ran Benita <ran@unusedvar.com>
  • Loading branch information
3 people committed Feb 28, 2022
1 parent 14b993d commit 8f4de08
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions docs/managing_python_path.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,39 @@ add this directly to your project's requirements.txt file like this::
pytest-django


Using pytest-pythonpath
~~~~~~~~~~~~~~~~~~~~~~~
Using pytest's ``pythonpath`` option
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can explicitly add paths to the Python search path using pytest's
:pytest-confval:`pythonpath` option.
This option is available since pytest 7; for older versions you can use the
`pytest-pythonpath <https://pypi.python.org/pypi/pytest-pythonpath>`_ plugin.

Example: project with src layout
````````````````````````````````

For a Django package using the ``src`` layout, with test settings located in a
``tests`` package at the top level::

myproj
├── pytest.ini
├── src
│ └── myproj
│ ├── __init__.py
│ └── main.py
└── tests
├── testapp
| ├── __init__.py
| └── apps.py
├── __init__.py
├── settings.py
└── test_main.py

You'll need to specify both the top level directory and ``src`` for things to work::

You can also use the `pytest-pythonpath
<https://pypi.python.org/pypi/pytest-pythonpath>`_ plugin to explicitly add paths to
the Python path.
[pytest]
DJANGO_SETTINGS_MODULE = tests.settings
pythonpath = . src

If you don't specify ``.``, the settings module won't be found and
you'll get an import error: ``ImportError: No module named 'tests'``.

0 comments on commit 8f4de08

Please sign in to comment.