matplotlib.pyplot错误" ImportError:无法导入名称' _path'"

时间:2018-04-07 21:55:00

标签: python python-3.x matplotlib

我在Windows 10 Pro上导入matplotlib.pyplot时遇到了ImportError

以下是完整错误:

Traceback (most recent call last):
  File "C:\Users\****\Dropbox\Code\Python\lib_test.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\pyplot.py", line 31, in <module>
    import matplotlib.colorbar
  File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\colorbar.py", line 32, in <module>
    import matplotlib.artist as martist
  File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py", line 16, in <module>
    from .path import Path
  File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\path.py", line 21, in <module>
    from . import _path, rcParams
ImportError: cannot import name '_path'

这是脚本:

import matplotlib.pyplot as plt

squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()

如果有人能帮助我,非常感谢。

3 个答案:

答案 0 :(得分:0)

Dunno什么是mpip,或者为什么,试试普通的老pip(或者在你的情况下也许是pip3?这可能更多地与基础Python的安装方式有关,为什么我建议使用Anaconda来安装所有(它也自动安装MPL ..)

答案 1 :(得分:0)

这可能是与matplotlib版本有关的问题。我遇到了同样的问题,并解决了该问题,并卸载了现有版本的matplotlib(在我的情况下,使用 conda ,但命令类似于替换 pip conda ),这样: 首先使用以下方法进行卸载:

$encodings = array(
    "UCS-4",
    "UCS-4BE",
    "UCS-4LE",
    "UCS-2",
    "UCS-2BE",
    "UCS-2LE",
    "UTF-32",
    "UTF-32BE",
    "UTF-32LE",
    "UTF-16",
    "UTF-16BE",
    "UTF-16LE",
    "UTF-7",
    "UTF7-IMAP",
    "UTF-8",
    "ASCII",
    "EUC-JP",
    "SJIS",
    "eucJP-win",
    "SJIS-win",
    "ISO-2022-JP",
    "ISO-2022-JP-MS",
    "CP932",
    "CP51932",
    "SJIS-mac",
    "SJIS-Mobile#DOCOMO",
    "SJIS-Mobile#KDDI",
    "SJIS-Mobile#SOFTBANK",
    "UTF-8-Mobile#DOCOMO",
    "UTF-8-Mobile#KDDI-A",
    "UTF-8-Mobile#KDDI-B",
    "UTF-8-Mobile#SOFTBANK",
    "ISO-2022-JP-MOBILE#KDDI",
    "JIS",
    "JIS-ms",
    "CP50220",
    "CP50220raw",
    "CP50221",
    "CP50222",
    "ISO-8859-1",
    "ISO-8859-2",
    "ISO-8859-3",
    "ISO-8859-4",
    "ISO-8859-5",
    "ISO-8859-6",
    "ISO-8859-7",
    "ISO-8859-8",
    "ISO-8859-9",
    "ISO-8859-10",
    "ISO-8859-13",
    "ISO-8859-14",
    "ISO-8859-15",
    "ISO-8859-16",
    "byte2be",
    "byte2le",
    "byte4be",
    "byte4le",
    "BASE64",
    "HTML-ENTITIES",
    "7bit",
    "8bit",
    "EUC-CN",
    "CP936",
    "GB18030",
    "HZ",
    "EUC-TW",
    "CP950",
    "BIG-5",
    "EUC-KR",
    "UHC",
    "ISO-2022-KR",
    "Windows-1251",
    "Windows-1252",
    "CP866",
    "KOI8-R",
    "KOI8-U",
    "ArmSCII-8"
);

$show_wrong_encodings = false;
$internal_encoding = "UTF-8";
mb_internal_encoding($internal_encoding);

$languages = array(
    // name of the language => hex in UTF-8 and timestamp to check
    "german" => array("4dc3a4727a", 1520343439), // march
    "french" => array("64c3a963656d627265", 1544103703), // december
    "polish" => array("677275647a6965c584", 1544103703), // december
    "turkish" => array("4172616cc4b16b", 1544103703), // december
    "hungarian" => array("6fc5be756a616b", 1520343439), // march
    "chinese" => array("e58d81e4ba8ce69c88", 1544103703), // december
    "russian" => array("d0b4d095d099d0aed090d09fd0ad", 1544103703) // december
);

$format = "%B"; // print full month name
print("<h1>Detecting the font encoding of <code>strftime()</code></h1>\n");

foreach($languages as $language => $data){
    // the hex value in UTF-8, this is the target value
    $hex = $data[0];
    // the timestamp to check
    $timestamp = $data[1];

    print(
        "<h2>".$language."</h2>\n".
        "<p>".
            "<code>strftime()</code> for ".formatTime("%B", "english", $timestamp)." ".
            "for language ".$language.". Expected hex:  <code>".$hex."</code>, converted expected ".
            "hex to string: <code>".tostring($hex)."</code>".
        "</p>\n"
    );

    // this is a different formatTime() function than mentioned above, it is defined after this 
    // foreach
    $string = formatTime("%B", $language, $timestamp);

    print("<table>\n");
    print("<tr>\n".
            "\t<td>initial return value</td>\n".
            "\t<td>".$string."</td>\n".
            "\t<td>".tohex($string)."</td>\n".
        "</tr>\n\n".
        "<tr><td colspan='3'>Encodings that deliver the correct result:</td></tr>"
    );

    foreach($encodings as $source_encoding){
        $converted = mb_convert_encoding($string, $internal_encoding, $source_encoding);
        $converted_hex = tohex($converted);

        $style = "";
        if($converted_hex == $hex){
            $style = "background: green";
        }
        elseif(!$show_wrong_encodings){
            $style = "display: none";
        }

        print("<tr style='".$style.";'>\n".
                "\t<td>".$source_encoding."</td>\n".
                "\t<td>".$converted."</td>\n".
                "\t<td>".$converted_hex."</td>\n".
            "</tr>\n"
        );
    }
    print("</table>");
}

function tohex($string){
    return implode(unpack("H*", $string));
}

function tostring($hex){
    return pack("H*", $hex);
}

function formatTime($format, $language, $timestamp){
    switch($language){
        case 'chinese':
            $locale = setlocale(LC_TIME, 'zh_CN.utf8', 'zh_CN.UTF-8', 'zh_CN', 'zh');
            break;
        case 'hungarian':
            $locale = setlocale(LC_TIME, 'hu_HU.utf8', 'hu_HU.UTF-8', 'hu_HU', 'hr');
            break;
        case 'russian':
            $locale = setlocale(LC_TIME, 'ru_RU.utf8', 'ru_RU.UTF-8', 'ru_RU', 'ru');
            break;
        case 'german':
            $locale = setlocale(LC_TIME, 'de_DE.utf8', 'de_DE.UTF-8', 'de_DE', 'de');
            break;
        case 'french':
            $locale = setlocale(LC_TIME, 'fr_FR.utf8', 'fr_FR.UTF-8', 'fr_FR', 'fr');
            break;
        case 'polish':
            $locale = setlocale(LC_TIME, 'pl_PL.utf8', 'pl_PL.UTF-8', 'pl_PL', 'pl');
            break;
        case 'turkish':
            $locale = setlocale(LC_TIME, 'tr_TR.utf8', 'tr_TR.UTF-8', 'tr_TR', 'tr');
            break;
        // ...
        default:
            $locale = setlocale(LC_TIME, 'en_GB.utf8', 'en_GB.UTF-8', 'en_GB', 'en');
            break;
    }

    $datetime = strftime($format, $timestamp);
    setlocale(LC_TIME, $locale);

    return $datetime;
}

,并且在成功删除现有版本(我的版本是2.2.2)之后,以下命令安装了一个后续版本,可以在模块名称后使用'=='进行指定:

conda uninstall matplotlib (or pip uninstall matplotlib)

,错误消失。 卸载命令也删除了 seaborn 安装,因此有必要在matplotlib之后安装它,但这很容易... 是的!:)

答案 2 :(得分:0)

由于您可能像我一样,并且做了很多installing/importing,因此在原始matplotlib安装中可能只是个不幸的事,所以请尝试让OL'出去,走走往后踢一脚...

conda uninstall matplotlib
#let it do its thing

conda install matplotlib
#let it do its thing

然后在您要执行的代码上对其进行测试,以查看其是否有效。就我而言,这归结为我的原始安装中的一个"_Path"问题,弄乱了事情。 uninstall/install之后,我很好。