Search code examples
pythonpipscipy

How to pip install from a text file skipping unreachable libraries


I'm currently in a scenario where I need to install the following:

pip install -r https://raw.githubusercontent.com/intro-stat-learning/ISLP_labs/v2.1.3/requirements.txt

What happens is that the library scipy is not available for some reason, resulting in the following error:

(test_islp) PS C:\Users\project> pip install -r https://raw.githubusercontent.com/intro-stat-learning/ISLP_labs/v2.1.3/requirements.txt 
Collecting numpy==1.24.2 (from -r https://raw.githubusercontent.com/intro-stat-learning/ISLP_labs/v2.1.3/requirements.txt (line 1))
...
...
ERROR: No matching distribution found for scipy==1.11.1

Is there a way to simply ignore libraries that return such error, and still go on with the installation of the remaining libraries?

I tried using --ignore-installed but it still leads to the same error

The quick and dirty solution would be to simply copy and paste the requirements into a text file, delete scipy and perform pip install again. That is not ideal for me because in a scenario with hundreds of libraries and dozens of potential missing distributions it could lead to tedious work.


Solution

  • You can use this simple python script to loop through all the packages in the requirements.txt and install them. As far as I can tell, this works on Linux and Windows, not sure about Mac.

    import subprocess
    
    def install_packages(requirements_file):
        with open(requirements_file, 'r') as file:
            packages = file.readlines()
    
        for package in packages:
            package = package.strip()
            try:
                subprocess.check_call(['pip', 'install', package])
            except subprocess.CalledProcessError as e:
                print(f"Error installing package: {package}")
    
    if __name__ == '__main__':
        requirements_file = 'requirements.txt'
        install_packages(requirements_file)