Adding a directory to sys.path with pathlib

From the docs:

A program is free to modify this list for its own purposes. Only strings should be added to sys.path; all other data types are ignored during import.

Add the path as a string to sys.path:

PROJECT_DIR = Path(__file__).parents[2]
sys.path.append(
    str(PROJECT_DIR / 'apps')
)

PROJECT_DIR is an instance of PosixPath which has all the goodies like / and .parents etc. You need to convert it to a string if you want to append it to sys.path.

Leave a Comment

tech