Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 749
lofidevops Avatar asked Aug 11 '26 07:08

lofidevops


1 Answers

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.

like image 115
lofidevops Avatar answered Aug 13 '26 23:08

lofidevops



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!