简体   繁体   中英

Error while Importing matplotlib.pyplot in python

Yesterday I was trying to write a program in which I had to import 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.

As you can see in your traceback, when importing matplotlib , matplotlib itself is importing other modules as well. In the last mentioned 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 . Now, your import error says:

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

string.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). 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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