Discussion:
dh_python for single python scripts?
(too old to reply)
Philippe Cerfon
2024-08-16 01:10:01 UTC
Permalink
Hey.

I'm already using dh_python for (Python) packages where I have a
pyproject.toml with some [project.scripts] section and use
python3-setuptools for building, which works quite nicely.


Now I do have some standalone python scripts for which it makes not
much sense to make them (Python) packages (i.e. there's no
__init__.py, etc.), yet I'd still like to have stuff like
automatically getting the right Debian package dependencies in a
Debian package.

Is that even intended to work with dh_python?

If so, how does it work, cause so far I didn't manage to get it working.
AFAIU using [project.scripts] section doesn't work, as I have no
(Python) package.

I tried with:
[tool.setuptools]
script-files = ["src/my-script.py"]


While that builds me a .deb:
a) script-files is discouraged per setuptools documentation
b) the Debian packge has none of the python packages/modules that I
import auto-detected as dependencies
c) After installing the package, tools like ptpyhon think there'd be a
my-script (notice the hyphen) package (i.e. when I type import my it
shows me my-script as auto-completion suggestion).
Not sure where that comes from, cause there's only:
└── usr
├── bin
│ └── my-script.py
├── lib
│ └── python3
│ └── dist-packages
│ └── my_script-1.0.0.dist-info
│ ├── INSTALLER
│ ├── METADATA
│ ├── WHEEL
│ └── top_level.txt

perhaps from the py3compile in the postinst?


Thanks for any help :-)

Philippe
Andrey Rakhmatullin
2024-08-16 06:10:02 UTC
Permalink
Post by Philippe Cerfon
Hey.
I'm already using dh_python for (Python) packages where I have a
pyproject.toml with some [project.scripts] section and use
python3-setuptools for building, which works quite nicely.
Now I do have some standalone python scripts for which it makes not
much sense to make them (Python) packages (i.e. there's no
__init__.py, etc.), yet I'd still like to have stuff like
automatically getting the right Debian package dependencies in a
Debian package.
If there is no metadata there is no "automatically getting the right
Debian package dependencies".
Post by Philippe Cerfon
b) the Debian packge has none of the python packages/modules that I
import auto-detected as dependencies
This is not done even for module packages.
--
WBR, wRAR
Stefano Rivera
2024-08-16 07:50:01 UTC
Permalink
Hi Philippe (2024.08.16_00:52:25_+0000)
Post by Philippe Cerfon
Now I do have some standalone python scripts for which it makes not
much sense to make them (Python) packages (i.e. there's no
__init__.py, etc.), yet I'd still like to have stuff like
automatically getting the right Debian package dependencies in a
Debian package.
Is that even intended to work with dh_python?
dh_python doesn't care what form your python package is in. It just
looks for the metadata in .dist-info / .egg-info.

So, if your package declares dependencies in in the packaging, and
installs correctly with python3-build, it'll work.

Stefano
--
Stefano Rivera
http://tumbleweed.org.za/
+1 415 683 3272
Philippe Cerfon
2024-08-16 16:50:01 UTC
Permalink
Hey Stefano.

(Thanks, also Andrey).
Post by Stefano Rivera
Post by Philippe Cerfon
Is that even intended to work with dh_python?
dh_python doesn't care what form your python package is in. It just
looks for the metadata in .dist-info / .egg-info.
Then there must be something I do wrong.
What I have is:
- src/my-script-file.py
(yes, hyphens here, which would of course be bad if it were a
package - maybe this is the reason for my troubles?)
- pyproject.toml
[project]
name = "my-script"
requires-python = ">=3.11"
dynamic = ["version"]
dependencies = [
"numpy"
]

[build-system]
requires = ["setuptools>=61.0.0", "setuptools_scm[toml]>=6.2.0"]
build-backend = "setuptools.build_meta"

[tool.setuptools]
script-files = ["src/my-script-file.py"]

[tool.setuptools_scm]


At first I tried:
[project.scripts]
my-script = "my-script:main"
instead of the
script-files = ["src/my-script-file.py"]

But that didn't really work (which I guess is no surprise) as
project.scripts expects a packge/module to load from, but my-script is
none.

With script-files = ["src/my-script-file.py"], the result from:
python3 -m build --no-isolation
*does* include my script at, and the Debian package also places it in
/usr/bin - but I somehow get that my-script-file "into" Python.

"into Python" is what I wrote before, namely when I run ptpython and
use the completion of import my... it shows me my-script-file as if it
were a module.
So I think something is still wrong, but I cannot find out what.

With the above pyproject.toml, the Debian package would contain:
drwxr-xr-x root/root 0 2024-08-15 23:56 ./
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/bin/
-rwxr-xr-x root/root 109240 2024-08-15 23:56 ./usr/bin/my-script-file.py
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/lib/
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/lib/python3/
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/lib/python3/dist-packages/
drwxr-xr-x root/root 0 2024-08-15 23:56
./usr/lib/python3/dist-packages/my_script_file-1.0.0.dist-info/
-rw-r--r-- root/root 7 2024-08-15 23:56
./usr/lib/python3/dist-packages/my_script_file-1.0.0.dist-info/INSTALLER
-rw-r--r-- root/root 1824 2024-08-15 23:56
./usr/lib/python3/dist-packages/my_script_file-1.0.0.dist-info/METADATA
-rw-r--r-- root/root 91 2024-08-15 23:56
./usr/lib/python3/dist-packages/my_script_file-1.0.0.dist-info/WHEEL
-rw-r--r-- root/root 1 2024-08-15 23:56
./usr/lib/python3/dist-packages/my_script_file-1.0.0.dist-info/top_level.txt
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/share/
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/share/doc/
drwxr-xr-x root/root 0 2024-08-15 23:56 ./usr/share/doc/my-script-file/
-rw-r--r-- root/root 169 2024-08-15 23:56
./usr/share/doc/my-script-file/changelog.Debian.gz
-rw-r--r-- root/root 1373 2024-08-15 23:56
./usr/share/doc/my-script-file/copyright

So it retains the hypens, execpt for the dir in dist-packages, where
it replaces them with _.

It also generates a postinst:
#!/bin/sh
set -e

# Automatically added by dh_python3
if command -v py3compile >/dev/null 2>&1; then
py3compile -p my-script-file
fi
if command -v pypy3compile >/dev/null 2>&1; then
pypy3compile -p my-script-file || true
fi

# End automatically added section

Which I guess makes no sense (as there is no such package), or does it?
And accordingly a prerm.

btw: When I rename the src/my-script-file.py to src/my-script-file (no
extension), then the contents of the .deb look the same, but the
postinst/prerm are gone.

Still I fail to understand, where that auto-completed my-script-file
comes from in ptpython.
Any ideas what I'm doing wrong?
Post by Stefano Rivera
So, if your package declares dependencies in in the packaging, and
installs correctly with python3-build, it'll work.
I had some stupid mistake in my pyproject.toml, after noticing that,
it correctly "auto-detected" the dependencies from that and translated
them into Debian package names :-)


Thanks,
Philippe
Philippe Cerfon
2024-08-16 21:20:01 UTC
Permalink
Hey again
Post by Philippe Cerfon
Still I fail to understand, where that auto-completed my-script-file
comes from in ptpython.
Any ideas what I'm doing wrong?
Maybe it's not me doing something wrong.
I have:
ls -al /usr/bin/*.py
lrwxrwxrwx 1 root root 45 Jun 25 00:44
/usr/bin/clang-tidy-diff-16.py ->
../lib/llvm-16/share/clang/clang-tidy-diff.py
-rwxr-xr-x 1 root root 15k Jan 4 2024 /usr/bin/lsusb.py
-rwxr-xr-x 1 root root 5,8k Aug 14 19:31 /usr/bin/mesa-overlay-control.py
-rwxr-xr-x 1 root root 3,5k Aug 12 2021 /usr/bin/pfs_split_exposures.py
lrwxrwxrwx 1 root root 33 Jun 25 00:44 /usr/bin/run-clang-tidy-16.py
-> ../lib/llvm-16/bin/run-clang-tidy

And all these names are also auto-completed in ptpython when trying to import.


Maybe what's left, though, are these py3compile calls, which - I guess
dh_python - makes, but which are probably not working in my case?


Thanks,
Philippe.
Andrey Rakhmatullin
2024-08-17 07:00:01 UTC
Permalink
Post by Philippe Cerfon
Hey again
Post by Philippe Cerfon
Still I fail to understand, where that auto-completed my-script-file
comes from in ptpython.
Any ideas what I'm doing wrong?
Maybe it's not me doing something wrong.
ls -al /usr/bin/*.py
This is against the Policy 10.4 by the way: "When scripts are installed
into a directory in the system PATH, the script name should not include an
extension such as .sh or .pl that denotes the scripting language currently
used to implement it."
--
WBR, wRAR
Loading...