import logging
import subprocess
def pip_install(package=None, req=None, dest=None, src_dir=None):
"""
pip安装命令
:param package: pip包名称,或者wheel包名称
:param dest: 安装目的位置
:return: src_dir: 其他依赖包的来源
"""
if package:
cmd_list = [PYTHON_ENV, '-m', 'pip', 'install', package]
elif req:
cmd_list = [PYTHON_ENV, '-m', 'pip', 'install', '-r', req]
else:
return False
if dest is not None:
cmd_list.append('--target')
cmd_list.append(dest)
if src_dir is not None:
cmd_list.append('--no-index')
cmd_list.append(f'--find-links=./')
try:
# 如果已经安装了也不会报错
out_bytes = subprocess.check_output(cmd_list)
logging.info(out_bytes.decode())
return True
except subprocess.CalledProcessError as e:
logging.error(f'install {package} failed, return={e.returncode}, error:{e.output}')
return False
实用小程序:pip install
最新推荐文章于 2024-04-11 21:24:18 发布