我使用的代码是:
import pip def install(package_name): try: pip.main(['install',package_name]) except: print("Unable to install " + package_name)
此代码工作正常,如果包不可用,则会出错:
No matching distributions found
但是,我想要做的是如果安装失败(例如:无效的包名),我想打印失败的包.
可以做些什么呢?
任何帮助将不胜感激,谢谢.
import pip def install(package_name): try: pipcode = pip.main(['install',package_name]) if pipcode != 0: print("Unable to install " + package_name + " ; pipcode %d" % pipcode) except: print("Unable to install " + package_name)