Search code examples
pythonpippypi

How to check if a pip package exists in certain given custom index?


I've to check whether a package exists in the given index-url (authenticated) using python script.

For example:

I've to check if package package-1 exists in index https://mytestdomain.com/pypi/pypi/simple/

Is there any method to achieve this?

What I've tried?

I've tried the cli method, like configuring pip.conf with the above index-url and using pip download <package_name>


Solution

  • Use subprocess.run with the --no-deps and --dry-run options:

    import subprocess, sys
    
    def check_exists(package_name: str, index_url: str = "https://pypi.org/simple") -> bool:
        return not subprocess.run(
            [
                sys.executable,
                "-m",
                "pip",
                "install",
                "--dry-run",
                "--no-deps",
                "--index-url",
                index_url,
                package_namee,
            ],
            capture_output=True,
        ).returncode