我正在尝试使用循环通过python代码安装一些软件包,但是一次只能安装一个软件包。
了解下一个软件包的错误。
Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'c:\\users\\akshay~1.sha\\appdata\\local\\temp\\pip-req-tracker-aoybzz\\f37aad13bd20c8b1e66a004a7c01edf203f7e46ba784c9954fe935f7'
如果再次运行该脚本,它将安装第二个软件包,并为第三个抛出错误。如果我第三次运行它,它将安装第三个,我想一次安装所有软件包,请帮忙。下面是代码
================================================ ==========================
import os
from pip._internal import main as pipmain
PACKAGES = {'pymongo': 'pymongo-3.4.0', 'pexpect': 'pexpect-4.6.0', 'pathlib': 'pathlib-1.0.1'}
def install_dependency(pkg_name, crnt_path):
pkg_path = os.path.join(crnt_path, "dependencies\\"+pkg_name+"\\")
print("Installing package from local...", pkg_name)
pipmain(["install", pkg_path])
def check_dependecies():
current_path = os.path.dirname(os.path.abspath(__file__))
print(PACKAGES.keys())
for package_name in PACKAGES.keys():
try:
__import__(package_name, globals=globals())
print("================> Package imported successfully", package_name)
except ImportError as import_error:
print("Package not present", import_error)
install_dependency(PACKAGES[package_name], current_path)
print("================> Package imported successfully", package_name)
__import__(package_name, globals=globals())
if __name__ == "__main__":
check_dependecies()
答案 0 :(得分:0)
我遇到了同样的问题,并且已经通过解决方法解决了这个问题。
我们知道我们不能使用pip在for循环中安装多个软件包,但是我们也知道我们可以一次安装多个软件包!
这就是我的前进方式:
{pkg_name: pkg_id}
中(例如:{'pymongo': 'pymongo-3.4.0'}
)pkg2install
(例如:pkg_id
)组成的列表['pymongo-3.4.0', 'pexpect-4.6.0']
pipmain
一键安装它们:`pipmain(['install',* pkg2install])您检查的代码:
from pip._internal import main as pipmain
PACKAGES = {'pymongo': 'pymongo', 'pexpect': 'pexpect', 'pathlib': 'pathlib'}
def check_dependecies():
missing_pkgs = {}
for package_name in PACKAGES.keys():
try:
__import__(package_name, globals=globals())
print("================> Package imported successfully", package_name)
except ImportError as import_error:
print("Package not present", import_error)
missing_pkgs.update({package_name: PACKAGES[package_name]})
pkgs2install = missing_pkgs.values()
status = pipmain(["install", *pkgs2install])
if status == 0:
print("================> Missing packages installed successfully", pkgs2install)
for pkg in missing_pkgs.keys():
__import__(pkg, globals=globals())
print("================> Package imported successfully", pkg)
if __name__ == "__main__":
check_dependecies()
它生成的输出:
Package not present No module named 'pymongo'
Package not present No module named 'pexpect'
================> Package imported successfully pathlib
Collecting pymongo
Downloading https://files.pythonhosted.org/packages/a3/8c/ec46f4aa95515989711a7893e64c30f9d33c58eaccc01f8f37c4513739a2/pymongo-3.9.0-cp37-cp37m-macosx_10_6_intel.whl (378kB)
100% |████████████████████████████████| 378kB 4.4MB/s
Collecting pexpect
Using cached https://files.pythonhosted.org/packages/0e/3e/377007e3f36ec42f1b84ec322ee12141a9e10d808312e5738f52f80a232c/pexpect-4.7.0-py2.py3-none-any.whl
Collecting ptyprocess>=0.5 (from pexpect)
Using cached https://files.pythonhosted.org/packages/d1/29/605c2cc68a9992d18dada28206eeada56ea4bd07a239669da41674648b6f/ptyprocess-0.6.0-py2.py3-none-any.whl
Installing collected packages: pymongo, ptyprocess, pexpect
Successfully installed pexpect-4.7.0 ptyprocess-0.6.0 pymongo-3.9.0
================> Missing packages installed successfully dict_values(['pymongo', 'pexpect'])
================> Package imported successfully pymongo
================> Package imported successfully pexpect
注意:我没有使用版本或本地软件包,但机制应保持不变
尽管希望迟到11个月,但我希望这会有所帮助。