Search code examples
pythonpython-3.xpipsubprocess

Python 3.9.6 - subprocess.run - subprocess.CalledProcessError: Command pip install pkg returned non-zero exit status 1


Python 3.9.6 - subprocess.run - subprocess.CalledProcessError: Command '['/opt/disk1/apps/venv_test/bin/python', '-m', 'pip', 'install pkg', '-q']' returned non-zero exit status 1.

Using Python 3.9.6, unable to get the below code executed and its throwing error for --pkg-- package. Error is given at below.

However, when trying to run the same command manually on linux console

command -> /opt/disk1/apps/venv_test/bin/python -m pip install pkg -q

then

  • this manual execution completed successfully
  • --pkg-- package got installed as well and
  • after this manual execution, when trying to execute below piece of code then its not throwing this or any other error as well

*Please advice why the code is not able to get executed without installing pkg manually?

Let me know if any additional info is required along with way to get this please.*

Code giving this error ->

  packages = ['fileinput', 'glob', 'gzip', 'hashlib', 'json', 'paramiko', 'pkg', 'psutil', 're', 'requests',
              'shutil',
              'socket']
  [subprocess.run([sys.executable, '-m', 'pip', 'install ' + pckg, '-q'], check=True) for pckg in packages if
   not importlib.util.find_spec(pckg)]

Error: -

ERROR: unknown command "install pkg" - maybe you meant "install"

Traceback (most recent call last):
  File "/opt/disk1/apps/bookcomponents.py", line 653, in <module>
    get_configurations_and_extract_info(redownload=False, kill_restart_required=True,
  File "/opt/disk1/apps/bookcomponents.py", line 504, in get_configurations_and_extract_info
    [subprocess.run([sys.executable, '-m', 'pip', 'install ' + pckg, '-q'], check=True) for pckg in packages if
  File "/opt/disk1/apps/bookcomponents.py", line 504, in <listcomp>
    [subprocess.run([sys.executable, '-m', 'pip', 'install ' + pckg, '-q'], check=True) for pckg in packages if
  File "/usr/lib64/python3.9/subprocess.py", line 528, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/opt/disk1/apps/venv_test/bin/python', '-m', 'pip', 'install pkg', '-q']' returned non-zero exit status 1.
(venv_test) [login@server:/opt/disk1/apps/Book/05Aug2022]$  pip list | grep pkg
WARNING: You are using pip version 22.0.4; however, version 22.2.2 is available.
You should consider upgrading via the '/opt/disk1/apps/venv_test/bin/python3 -m pip install --upgrade pip' command.
(venv_test) [login@server:/opt/disk1/apps/Book/05Aug2022]$  python --version
Python 3.9.6


Solution

  • You shouldn't concatenate the package name with the install keyword. You're executing

    php -m pip 'install fileinput' -q
    

    instead of

    php -m pip install fileinput -q
    

    install and pckg should be separate arguments, not a single concatenated string.

      [subprocess.run([sys.executable, '-m', 'pip', 'install', pckg, '-q'], check=True) for pckg in packages if
        not importlib.util.find_spec(pckg)]
    

    BTW, it's not pythonic to use a list comprehension if you're not doing anything with the resulting list. Use an ordinary for loop.

    for pck in packages:
        if not importlib.util.find_spec(pckg):
            subprocess.run([sys.executable, '-m', 'pip', 'install', pckg, '-q'], check=True)