简体   繁体   English

在 python 中导入 matplotlib.pyplot 时出错

[英]Error while Importing matplotlib.pyplot in python

Yesterday I was trying to write a program in which I had to import matplotlib .昨天我试图编写一个程序,我必须在其中导入matplotlib So when I tried to run the program after importing it it started to run some other program instead of the program I wrote and after running that other program it showed an error:因此,当我在导入程序后尝试运行程序时,它开始运行其他程序而不是我编写的程序,并且在运行其他程序后显示错误:

Traceback (most recent call last):
  File "E:\Kartikay\kartik py\excercise.py", line 2, in <module>
    import matplotlib.pyplot as plt
  File "C:\Users\Garg's\AppData\Local\Programs\Python\Python37-32\lib\site-packa
ges\matplotlib\__init__.py", line 127, in <module>
    import logging
  File "C:\Users\Garg's\AppData\Local\Programs\Python\Python37-32\lib\logging\__
init__.py", line 28, in <module>
    from string import Template
ImportError: cannot import name 'Template' from 'string' (E:\Kartikay\kartik py\
string.py)

So can anyone please help me in why this is happening and how can I fix it.所以任何人都可以请帮助我为什么会发生这种情况以及我该如何解决它。

Delete or rename E:\Kartikay\kartik py\string.py , as it is clashing with another module.删除或重命名E:\Kartikay\kartik py\string.py ,因为它与另一个模块发生冲突。

As you can see in your traceback, when importing matplotlib , matplotlib itself is importing other modules as well.正如您在回溯中看到的,在导入matplotlib时, matplotlib 本身也在导入其他模块。 In the last mentioned File :在最后提到的File中:

 C:\Users\Garg's\AppData\Local\Programs\Python\Python37-32\lib\logging\__init__.py

(which is the entry point of the built in logging module), there is an from string import Template . (这是内置logging模块的入口点),有一个from string import Template Now, your import error says:现在,您的导入错误说:

cannot import name 'Template' from 'string' (E:\Kartikay\kartik py\

string.py)字符串.py)

Which means: the line from string import Template that is in some built in module is trying to import string , but since you have created a file with that name, it is trying to read from there (files in your current directory are always searched first when importing).这意味着: from string import Template的行正在尝试导入string ,但是由于您已经创建了一个具有该名称的文件,它正试图从那里读取(始终首先搜索当前目录中的文件导入时)。 Same goes for other imports.其他进口也是如此。 So take care how you name your files.因此,请注意如何命名文件。 If you notice that one of your files is being executed when you import matplotlib , then you should rename that file如果您注意到导入matplotlib时正在执行您的文件之一,那么您应该重命名该文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM