我需要在脚本中直接从PyPi安装一个包。 也许有一些模块或distutils(分发,pip等)功能,允许我只执行类似pypi.install('requests')的东西,请求将被安装到我的virtualenv中。
当前回答
试试下面的方法。到目前为止,对我来说是最好的 首先安装4个,然后在REQUIRED列表中提到新的
import pkg_resources
import subprocess
import sys
import os
REQUIRED = {
'spacy', 'scikit-learn', 'numpy', 'pandas', 'torch',
'pyfunctional', 'textblob', 'seaborn', 'matplotlib'
}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = REQUIRED - installed
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
其他回答
为了有条件地安装多个确切版本的包,我一直在使用基于@Tanmay Shrivastava的回答的这个模式:
import sys
from subprocess import run, PIPE, STDOUT
import pkg_resources
def run_cmd(cmd):
ps = run(cmd, stdout=PIPE, stderr=STDOUT, shell=True, text=True)
print(ps.stdout)
# packages to be conditionally installed with exact version
required = {"click==8.0.1", "semver==3.0.0.dev2"}
installed = {f"{pkg.key}=={pkg.version}" for pkg in pkg_resources.working_set}
missing = required - installed
if missing:
run_cmd(f'pip install --ignore-installed {" ".join([*missing])}')
如果你想要一个更有效的答案,扩展subprocess.check_call。您可以首先使用pkg_resources检查需求是否已经得到满足。
这适用于不同的需求说明符,这很好。例如>=,==
import sys
import subprocess
import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict
def should_install_requirement(requirement):
should_install = False
try:
pkg_resources.require(requirement)
except (DistributionNotFound, VersionConflict):
should_install = True
return should_install
def install_packages(requirement_list):
try:
requirements = [
requirement
for requirement in requirement_list
if should_install_requirement(requirement)
]
if len(requirements) > 0:
subprocess.check_call([sys.executable, "-m", "pip", "install", *requirements])
else:
print("Requirements already satisfied.")
except Exception as e:
print(e)
使用示例:
requirement_list = ['requests', 'httpx==0.18.2']
install_packages(requirement_list)
相关信息Stackoverflow问题:58612272
试试下面的方法。到目前为止,对我来说是最好的 首先安装4个,然后在REQUIRED列表中提到新的
import pkg_resources
import subprocess
import sys
import os
REQUIRED = {
'spacy', 'scikit-learn', 'numpy', 'pandas', 'torch',
'pyfunctional', 'textblob', 'seaborn', 'matplotlib'
}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = REQUIRED - installed
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
import pip
try:
import imaplib
import email
import pandas as pd
# for hiding password
from pathlib import Path
from dotenv import load_dotenv
import os
import requests
#
from collections import defaultdict
from itertools import permutations,combinations
except Exception as e:
print(e)
e = str(e).split(' ')[-1].replace("'","")
pip.main(['install', e])
import os
os.system('pip install requests')
我尝试了上述临时解决方案,而不是更改docker文件。希望这些可能对一些人有用
推荐文章
- 为什么我得到“'str'对象没有属性'读取'”当尝试使用' json。载入字符串?
- 不区分大小写的列表排序,没有降低结果?
- 排序后的语法(key=lambda:…)
- 在烧瓶中返回HTTP状态代码201
- 使用python创建一个简单的XML文件
- APT命令行界面式的yes/no输入?
- 如何打印出状态栏和百分比?
- 在Python中获取大文件的MD5哈希值
- 在Python格式字符串中%s是什么意思?
- 如何循环通过所有但最后一项的列表?
- python用什么方法避免默认参数为空列表?
- ValueError: numpy。Ndarray大小改变,可能表示二进制不兼容。期望从C头得到88,从PyObject得到80
- Anaconda /conda -安装特定的软件包版本
- 我在哪里调用Keras的BatchNormalization函数?
- 打印测试执行时间并使用py.test锁定缓慢的测试