Use dh_virtualenv with pyenv

Viewed 210

I'm trying to build a debian package from my python project using dh_virtualenv (https://github.com/spotify/dh-virtualenv)

I followed this step by step guide with a few modifications: https://camilomatajira.wordpress.com/2020/06/13/how-to-create-a-python-debian-package-with-dh-virtualenv/

I create a Dockerfile, but I add pyenv to it and install python 3.8.10. In the cookiecutter template I set the SNAKE variable to "python3" which gets resolved by the pyenv shim. The complete rules file:

export DH_VIRTUALENV_INSTALL_ROOT=/opt/venvs
SNAKE="python3"
EXTRA_REQUIREMENTS=--upgrade-pip --preinstall "setuptools>=38" --preinstall "wheel"
DH_VENV_ARGS=--builtin-venv --python=$(SNAKE) $(EXTRA_REQUIREMENTS) \
            --extra-pip-arg=--progress-bar=pretty
            # --extra-pip-arg "--no-binary=psycopg2" \
            # -v
PACKAGE=$(shell dh_listpackages)
VERSION=$(shell $(SNAKE) setup.py --version)
SDIST_DIR=debian/$(PACKAGE)-$(VERSION)


.PHONY: clean build-arch override_dh_virtualenv override_dh_strip
    # override_dh_shlibdeps

clean:
    test ! -d dist || rm -rf dist
    test ! -d $(SDIST_DIR) || rm -rf $(SDIST_DIR)
    dh $@ $(DH_VENV_ARGS)

build-arch:
    $(SNAKE) setup.py sdist --formats tar
    mkdir -p $(SDIST_DIR)
    tar -x -C $(SDIST_DIR) --strip-components=1 --exclude '*.egg-info' -f dist/*.tar
    dh $@ $(DH_VENV_ARGS) --sourcedir $(SDIST_DIR)

%:
    dh $@ --with python-virtualenv --sourcedir $(SDIST_DIR)

override_dh_virtualenv:
    dh_virtualenv $(DH_VENV_ARGS)

override_dh_strip:
    dh_strip --exclude=cffi --exclude=_imaging

In my setup.py I add an execution script like so:

setuptools.setup(
    name="myapp",
    version="0.9.0",
    package_dir={"": "."},
    packages=setuptools.find_packages(where=".", exclude=["tasks*", "test*"]),
    install_requires=["aiohttp"],
    python_requires=">=3.8",
    scripts=["bin/myapp"],
)

To build the package I run the regular dpkg-buildpackage -uc -us -b. I do get a finished .deb file from this and when I install it, the installation directory contains my virtualenv as expected. But I can't run the program, because the python version in the environment symbolically links to the absolute path of the pyenv version in my docker container, so in my case:

lrwxrwxrwx 1 root root    7 Jan  1  2014 python -> python3
lrwxrwxrwx 1 root root   40 Jan  1  2014 python3 -> /root/.pyenv/versions/3.8.10/bin/python3

How can I fix this?

0 Answers
Related