终端中的python3子进程pip“ImportError:无法导入名称main”

我创建了一个脚本(见下文)来升级我所有的 pip 包。我通过 成功执行了我的脚本idle3,即使用打开脚本idle3并按下F5以将脚本作为模块运行。但是,我无法在终端中执行它;得到以下错误。我如何克服这个错误?为什么导入错误发生在终端而不是在idle3?


$ python3 -m upgrade_pip_packages 

====================================================

UPGRADING ALL --USER PIP PACKAGES TO LATEST VERSION:

====================================================

Traceback (most recent call last):

  File "/usr/bin/pip", line 9, in <module>

    from pip import main

ImportError: cannot import name main

ERROR: Command 'pip list' returned non-zero exit status 1

我的脚本:upgrade_pip_packages.py


#!/bin/python3

import subprocess

from pprint import pprint



def get_pkgs():

    try:

        cmd = 'pip list'

        completed = subprocess.run( cmd, shell=True, check=True,

                                    stdout=subprocess.PIPE )

    except subprocess.CalledProcessError as err:

        print( 'ERROR:', err )

    else:

        for line in completed.stdout.decode('utf-8').splitlines()[2:]:

            yield line



def update_pkgs(piplist):

    npackages = 0

    nupgrades = 0

    nerrors = 0

    upgradelist = []

    errorlist = []

    for i in piplist:

        npackages += 1

        pkgname, ver = i.split()

        print('\n',pkgname)

        try:

            cmd = 'pip install --user {} --upgrade'.format(pkgname)

            completed = subprocess.run( cmd, shell=True, check=True,

                                        stdout=subprocess.PIPE )

        except subprocess.CalledProcessError as err:

            nerrors += 1

            errorlist.append(pkgname)

            print( 'ERROR: {}'.format(err) )

        else:

            for line in completed.stdout.decode('utf-8').splitlines():

                print(line)

                if 'Successfully installed' in line:

                    nupgrades +=1

                    upgradelist.append(pkgname)

    return npackages, nupgrades, nerrors, upgradelist, errorlist



aluckdog
浏览 223回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python