如何使用Python检查并安装库。

huangapple go评论68阅读模式
英文:

How to use python to check for and install library's

问题

以下是翻译好的部分:

"Is there a way to get this to work as every time I run it, It Won't give me an output, just 'PRESS ANY KEY TO CONTINUE...'

as this is currently a test to read the modules I would like it to print the results as well

all help is appreciated

Import the necessary packages

import pip

Create a list of packages to install

packages_to_install = ["numpy", "pandas", "matplotlib"]

List all of the installed packages

installed_packages = pip.freeze()

Compare the list of installed packages to the list of packages to install

for package in packages_to_install:
if package not in installed_packages:
# Print the List of uninstalled packages
print(package)"

英文:

Is there a way to get this to work as every time I run it, It Won't give me a output, just "PRESS ANY KEY TO CONTINUE..."
<br>

as this is currently a test to read the modules I would like it to print the results as well

all help is appreciated

# Import the necessary packages
import pip

# Create a list of packages to install
packages_to_install = [&quot;numpy&quot;, &quot;pandas&quot;, &quot;matplotlib&quot;]

# List all of the installed packages
installed_packages = pip.freeze()

# Compare the list of installed packages to the list of packages to install
for package in packages_to_install:
    if package not in installed_packages:
        # Print the List of uninstalled packages
        print(package)

答案1

得分: 0

试试这个:

import importlib
import subprocess
import sys

packages_to_install = ["numpy", "pandas", "matplotlib"]

def check_and_install_packages(packages):
    for package in packages:
        try:
            importlib.import_module(package)
            print(f"{package} 已安装。")
        except ImportError:
            print(f"{package} 未安装,正在安装...")
            subprocess.check_call([sys.executable, "-m", "pip", "install", package])
            print(f"{package} 安装成功。")

# 检查并安装需要的包
check_and_install_packages(packages_to_install)

# 现在你的脚本可以安全地使用已安装的包
英文:

try this:

import importlib
import subprocess
import sys

packages_to_install = [&quot;numpy&quot;, &quot;pandas&quot;, &quot;matplotlib&quot;]

def check_and_install_packages(packages):
    for package in packages:
        try:
            importlib.import_module(package)
            print(f&quot;{package} is already installed.&quot;)
        except ImportError:
            print(f&quot;{package} is not installed. Installing...&quot;)
            subprocess.check_call([sys.executable, &quot;-m&quot;, &quot;pip&quot;, &quot;install&quot;, package])
            print(f&quot;{package} installed successfully.&quot;)

# Check and install packages if needed
check_and_install_packages(packages_to_install)

# Rest of your script can now safely use the installed packages

huangapple
  • 本文由 发表于 2023年6月2日 08:19:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76386461.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定