3

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'
Larry Cai
  • 55,923
  • 34
  • 110
  • 156
lofidevops
  • 15,528
  • 14
  • 79
  • 119
  • 1
    This is not what I would call a `src`-layout. In a `src` layout there definitely should not be any `__init__.py` in the `src` directory. Because that would mean that `src` is an importable package (as in `import src`), which goes against the whole point of the `src`-layout in the first place. – sinoroc Mar 13 '23 at 11:48
  • 1
    @sinoroc doh! that was a transcription error on my part, fixed – lofidevops Mar 13 '23 at 11:51
  • 1
    Still... the way this question is formulated is confusing to me, it mixes two different things into one. From my point of view it has nothing to do with `src` layout or not. It has to do with restructuring the package structure by moving the `cli` module into a `mypackage` package. You could have a `src` layout while still keeping `cli` as a top-level module without moving it into a `mypackage` package`. – sinoroc Mar 13 '23 at 12:20

1 Answers1

2

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.

lofidevops
  • 15,528
  • 14
  • 79
  • 119
  • Hi! Do you have the reference for this? Where did you learn this? – Thiago Lages de Alencar Jun 14 '23 at 06:10
  • 1
    I learnt by bashing my head against it :D the references for pyprojects and scripts are a bit scattered - there is a good authoritative one but I don't have the handy :( - I maintain a barebones project https://gitlab.com/lfdo/mini/tcarlae that I update occasionally as I learn – lofidevops Jun 14 '23 at 20:48