How do I configure pdm for a src-based Python repository?

1.6k views Asked by At

I have previously arranged a Python repository without a src folder, and got it running with:

pdm install --dev
pdm run mymodule

I am failing to replicate the process in a repository with a src folder. How do I do it?

pyproject.toml

[project]
name = "mymodule"
version = "0.1.0"
description = "Minimal Python repository with a src layout."
requires-python = ">=3.10"

[build-system]
requires = ["pdm-pep517>=1.0.0"]
build-backend = "pdm.pep517.api"

[project.scripts]
mymodule = "cli:invoke"

src/mymodule/__init__.py

Empty file.

src/mymodule/cli.py

def invoke():
    print("Hello world!")


if __name__ == "__main__":
    invoke()

With the configuration above, I can pdm install --dev but pdm run mymodule fails with:

Traceback (most recent call last):
File "/home/user/Documents/mymodule/.venv/bin/mymodule", line 5, in <module>
    from cli import invoke
ModuleNotFoundError: No module named 'cli'
1

There are 1 answers

2
lofidevops On BEST ANSWER

You need to modify your pyproject.toml as follows:

[project.scripts]
mymodule = "mymodule.cli:invoke"

For good measure, you may want to delete the .venv folder and .pdm.toml file before running pdm install --dev again.