Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mgsanava opened this issue Sep 26, 2023 · 0 comments
Open

Comments

@mgsanava
Copy link

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)

@mgsanava mgsanava changed the title package is installed but script says it is not installed and says it is installed later package is installed but script says it is not installed via script Sep 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant