下面的代码使用不可用的版本(如)调用该软件包
pip installpackage_name==random。该调用将返回所有可用版本。该程序将读取最新版本。
然后,程序运行
pip show package_name并获取软件包的当前版本。
如果找到匹配项,则返回True,否则返回False。
考虑到它是一个可靠的选择
pip
import subprocessimport sysdef check(name): latest_version = str(subprocess.run([sys.executable, '-m', 'pip', 'install', '{}==random'.format(name)], capture_output=True, text=True)) latest_version = latest_version[latest_version.find('(from versions:')+15:] latest_version = latest_version[:latest_version.find(')')] latest_version = latest_version.replace(' ','').split(',')[-1] current_version = str(subprocess.run([sys.executable, '-m', 'pip', 'show', '{}'.format(name)], capture_output=True, text=True)) current_version = current_version[current_version.find('Version:')+8:] current_version = current_version[:current_version.find('\n')].replace(' ','') if latest_version == current_version: return True else: return False
以下代码要求
pip list --outdated:
import subprocessimport sysdef check(name): reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'list','--outdated']) outdated_packages = [r.depre().split('==')[0] for r in reqs.split()] return name in outdated_packages
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)