Skip to content

package is installed but script says it is not installed via script #27

Open
@mgsanava

Description

@mgsanava

Hi. Below is AI generated code to check if a certain python package is installed or not.
It checks for python-string-utils and jsonschema packages existance.
For python-string-utils it prints below EVERY TIME the script is ran:

python-string-utils is not installed or is not the desired version. Installing...
python-string-utils (1.0.0) has been successfully installed.

For jsonschema it prints below immediately:

jsonschema (4.17.3) is already installed.

What could be the issue that is causing python-string-utils to be checked always?
Any leads is appreciated.

Code:

import importlib
import subprocess

# Define a list of packages and their desired versions
packages_to_check = [
    {"name": "python-string-utils", "version": "1.0.0"},
    {"name": "jsonschema", "version": "4.17.3"},
]

for package_info in packages_to_check:
    package_name = package_info["name"]
    desired_version = package_info["version"]

    try:
        # Attempt to import the package
        importlib.import_module(package_name)
        print(f"{package_name} ({desired_version}) is already installed.")
    except ImportError:
        print(f"{package_name} is not installed or is not the desired version. Installing...")

        # Install the package with the desired version
        install_command = ["pip3", "install", f"{package_name}=={desired_version}"]

        # Run the installation command
        installation_result = subprocess.run(install_command, capture_output=True, text=True)

        if installation_result.returncode == 0:
            print(f"{package_name} ({desired_version}) has been successfully installed.")
        else:
            print(f"Failed to install {package_name} ({desired_version}).")
            print("Installation error output:")
            print(installation_result.stderr)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions