SoFunction
Updated on 2025-04-12

pip install -r  automatically skip it when encountering an error.

1. Problem background

When trying to copy an environment, export all Python libraries asFile, intended to be usedpip install -r Process installation. However, if a library installation fails, the program will be interrupted, for example,ERROR: Could not find a version that satisfies the requirement anaconda-navigatorError, causing subsequent libraries to be unable to be installed.

2. Reason

anaconda-navigatorNot throughpipInstalled, but installed through Anaconda or Miniconda. therefore,pipThe package cannot be found and an error was reported.

3. Solution

pipThere is no option to skip the error itself, but it can be implemented indirectly by:

use--ignore-installedand--no-depsOptions

pip install --ignore-installed --no-deps -r 
  • --ignore-installed: Ignore installed packages.
  • --no-deps: No dependency package is installed to reduce conflicts.

Increase the number of retry and timeout

pip install --retries 5 --timeout 60 -r 
  • --retries: Set the number of retry times.
  • --timeout: Set the timeout time.

Install one by one using Python scripts

Write scripts to install the library line by line and skip it when errors are encountered.

import subprocess

with open("", "r") as f:
    for line in f:
        package = ()
        if not package:
            continue
        try:
            print(f"Installing {package}...")
            subprocess.check_call(["pip", "install", package])
        except  as e:
            print(f"Error installing {package}: {e}")
            print("Skipping and continuing...")

usepip-toolsandpip-sync

Installpip-toolsAnd usepip-syncInstallation dependencies.

pip install pip-tools
pip-sync 

Manual edit

Comment out the uninstallable library:

# anaconda-navigator
numpy
pandas

Then rerun the installation command.

Further add logs

A new variable pip_source has been added to specify the installation source (such as Alibaba Cloud's mirror source);

If a library fails to install, write its name to the failed_packages.txt file;

If a library fails to install, write the error message to the failed_logs.txt file to facilitate subsequent troubleshooting.

import subprocess

# Define the file path to save failed libraries and logsfailed_packages_file = "failed_packages.txt"
failed_logs_file = "failed_logs.txt"

# Specify the installation sourcepip_source = "/pypi/simple/"

# Open the library and logs that are used to record failedwith open(failed_packages_file, "w", encoding="utf-8") as failed_packages_f, \
     open(failed_logs_file, "w", encoding="utf-8") as failed_logs_f:

    with open("", "r", encoding="utf-8") as f:
        for line in f:
            package = ()
            if not package:
                continue
            try:
                print(f"Installing {package}...")
                # Use the specified installation source                subprocess.check_call(["pip3", "install", package, "-i", pip_source])
                print(f"{package} Installation successfully!")
            except  as e:
                print(f"Install {package} An error occurred while: {e}")
                print("Skip and continue installing other libraries...")

                # Record failed library                failed_packages_f.write(package + "\n")

                # Log failed logs                failed_logs_f.write(f"Install {package} An error occurred while:\n")
                failed_logs_f.write(str(e) + "\n")
                failed_logs_f.write("-" * 50 + "\n")

print(f"Install失败的库已保存到 {failed_packages_file}")
print(f"Install失败的日志已保存到 {failed_logs_file}")

This is the article about automatically skipping when pip install -r is reported here. For more related pip install -r, please search for my previous article or continue browsing the related articles below. I hope everyone will support me in the future!