Use pathlib in `sys.path` manipulation examples

This commit is contained in:
Adam Turner
2024-11-03 22:54:00 +00:00
parent fbb23071bf
commit 1266c421be
4 changed files with 15 additions and 9 deletions

View File

@@ -313,10 +313,10 @@ For example:
.. code-block:: python
import os
import sys
from pathlib import Path
sys.path.append(os.path.abspath("./_ext"))
sys.path.append(str(Path('_ext').resolve()))
extensions = ['todo']

View File

@@ -169,10 +169,10 @@ For example:
.. code-block:: python
import os
import sys
from pathlib import Path
sys.path.append(os.path.abspath("./_ext"))
sys.path.append(str(Path('_ext').resolve()))
extensions = ['helloworld']

View File

@@ -223,11 +223,14 @@ General configuration
Ensure that absolute paths are used when modifying :data:`sys.path`.
If your custom extensions live in a directory that is relative to the
:term:`configuration directory`, use :func:`os.path.abspath` like so:
:term:`configuration directory`, use :meth:`pathlib.Path.resolve` like so:
.. code-block:: python
import os, sys; sys.path.append(os.path.abspath('sphinxext'))
import sys
from pathlib import Path
sys.path.append(str(Path('sphinxext').resolve()))
extensions = [
...

View File

@@ -69,11 +69,14 @@ Where to put your own extensions?
Extensions local to a project should be put within the project's directory
structure. Set Python's module search path, ``sys.path``, accordingly so that
Sphinx can find them. For example, if your extension ``foo.py`` lies in the
``exts`` subdirectory of the project root, put into :file:`conf.py`::
``exts`` subdirectory of the project root, put into :file:`conf.py`:
import sys, os
.. code-block:: python
sys.path.append(os.path.abspath('exts'))
import sys
from pathlib import Path
sys.path.append(str(Path('exts').resolve()))
extensions = ['foo']