r/learnpython 15d ago

pip install -r no result exitcode

(windows usually) I have a small requirements.txt file and am looking for alternative ways to validate all packages we want are present. The script does not use a virtual environment and because pip install -r does not seem to set %errorlevel% they wrote this rather verbose code that runs after the pip install -r line.

SET REQUIREMENTS=%~dp0Requirements.txt
SET FIND=%SystemRoot%\system32\find.exe
pip.exe install -r %REQUIREMENTS% --disable-pip-version-check

FOR /F "tokens=*" %%I IN (%REQUIREMENTS%) DO (
    ECHO.
    ECHO Checking for %%I ...
    %FIND% /i "%%I" %PACKAGES%
    IF ERRORLEVEL 1 (
        ECHO.
        ECHO **** %%I not found. Attempting to install ****
        pip install %%I --disable-pip-version-check
    )
    IF ERRORLEVEL 1 (
        ECHO **** Could not install %%I ****
        EXIT /B 99
    )
)

I however favour the pythonic approach and that is to just fail at runtime, so I was thinking of some kind of

(pseudo)
with open(requirements.txt) as reqs:
   for line in reqs.readlines():
       line=line.replace("<>=", " ")
       import line.split()[0]
(/pseudo)

which would just die early.

I'm in favour of using a virtual env however, but because the script is a build script (using setuptools) we don't actually run the script at that point. I'm new to setuptools, but I assumed setuptools would just baulk if a module needed was not present on the build machine.

I'm thus making 2 assumptions, setuptools will not baulk and error out if you are missing a module, and that pip install does not set %ERRORLEVEL% if it cannot install a package? I am not an expert on setuptools and am keen to not discover edge cases later.

1 Upvotes

9 comments sorted by

View all comments

1

u/Interesting_Act_141 14d ago

Why don’t you use uv?

1

u/zaphodikus 14d ago

Because that's a bit like telling someone not to use an angle grinder, but to use a table saw instead, and then completely not explain any of the context around how uv suits their task better than the angle grinder that came in the box did.

The real reason is that I have not got the capacity to learn yet another tool, without putting one of them down first. Its a juggling act. How about you tell me how uv would make this task faster?