Import Error: Cannot import name '_path' PYTHON - python

I know other people went through this kind of problem but I still can't fix the problem. I just began to programming.
I use spyder IDE and get the following error when I run the code.
File "C:\Users\kaany\AppData\Roaming\Python\Python36\site-packages\matplotlib\path.py", line 25, in <module>
from . import _path, rcParams
ImportError: cannot import name '_path'
When I remove 'import matplotlib as plt' from my code everything goes fine.
I have numpy 1.13.3 and matplotlib 2.1.0
I'd appreciate if someone provided any solution to this.

Related

Matplotlib is not working as it used to on pycharm. How can I solve it?

Note: I am just a beginner and doing this by myself, so if you can reply with basic descriptions, that could help more.
This is the code I wrote
from matplotlib import pyplot as plt
plt.plot([2, 1], [8, 2], 'ro')
However, I get this error. I had a code using same way to import and it used to work just fine. Now that code is not working either.
C:\Users\Tuna\PycharmProjects\start\venv\Scripts\python.exe C:/Users/Tuna/PycharmProjects/start/matplotlib.py
Traceback (most recent call last):
File "C:\Users\Tuna\PycharmProjects\start\matplotlib.py", line 1, in <module>
from matplotlib import pyplot as plt
File "C:\Users\Tuna\PycharmProjects\start\matplotlib.py", line 1, in <module>
from matplotlib import pyplot as plt
ImportError: cannot import name 'pyplot' from partially initialized module 'matplotlib' (most likely due to a circular import) (C:\Users\Tuna\PycharmProjects\start\matplotlib.py)
Process finished with exit code 1
To fix, you just need to rename your file to something that isn't matplotlib.py. Python thinks you are trying to import your own file matplotlib.py to use rather than the actual module and hence why you are getting a circular reference.
This should work fine if you have matplotlib installed, if not pycharm should prompt you to install it which should be the easiest way for you to use it in your project.

CentOS Matplotlib RuntimeError: Could not open display

I am using Cuda and run into a RuntimeError.
Environment:
Python 2.7
Cuda 9.0
Torch 0.4.0
Torchvision 0.1.8
Matplotlib 1.2.0
error message:
File "/usr/lib64/python2.7/site-packages/matplotlib/backends/backend_gtk.py", line 13, in <module>
import gtk; gdk = gtk.gdk
File "/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
_init()
File "/usr/lib64/python2.7/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
_gtk.init_check()
RuntimeError: could not open display
Adding "MPLBACKEND=Agg" still outputs the above error message.
Anyone knows how to fix it?
I said that I would close this question. But since the answer might be interesting to some people, I leave it open. Maybe someone can explain to me why my solution worked?
I work remotely. echo $DISPLAY gives nothing. lshw -C display gives two displays. One is Nvidia's.
My problem is actually solved via adding
import matplotlib as mpl
mpl.use('Agg')
before import matplotlib.pyplot as plt.

Python is returning an AttributeError while importing some modules all of sudden. How do I fix it?

My script was running well before. I had a series of crashes on some scripts where I had to fix some things. However, all of a sudden, I can't even run a script and my sublime text shell is displaying this error. I tried chasing down the files in my /Library/...python... files to see if any files got renamed or any part of the module scripts got renamed or something, but have yet to figure it out. What can I try. Or better question, what did I do all of a sudden to cause this, and how would I undo it? It was literally working fine less than an hour ago not to mention all day.
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numerictypes.py", line 101, in <module>
import numbers
File "/Users/peter/Documents/Trading/Code/AlphaModelVer1/numbers.py", line 2, in <module>
import Quandl
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Quandl/__init__.py", line 11, in <module>
from .Quandl import (
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Quandl/Quandl.py", line 11, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/__init__.py", line 7, in <module>
from . import hashtable, tslib, lib
File "pandas/src/numpy.pxd", line 157, in init pandas.hashtable (pandas/hashtable.c:22984)
AttributeError: 'module' object has no attribute 'dtype'
EDIT:
Thanks to the comments! I had tried re-installing pandas, numpy, and Quandl. But it was all due to the fact I had renamed a helper script numbers.py, when it needed to draw that from within. Hint to all future users: Do not rename python scripts to existing module types.
I've had a similar error that was caused by installing a package while I had IPython running. Try reinstalling Pandas using pip (make sure all Python instances are closed) and maybe you'll have to reiinstall hashtable as well, I can't remember.
try to reinstall this Quandl library
File
"/Users/peter/Documents/Trading/Code/AlphaModelVer1/numbers.py", line
2, in
import Quandl
pip uninstall Quandl
pip install Quandl
This problem occurred with me in spite of not naming the script name to the existing module name. I tried the following and it worked.
Instead of "import Quandl", I did "from Quandl import Quandl" and it worked for me!

Anaconda: Attribute error while running pylab with anaconda distribution on Windows

I am trying to import pylab and this is the code that I am writing.
import pylab
pylab.figure(1)
pylab.plot([1,2,3,4],[1,7,3,5])
pylab.show()
But I am getting the error that
File "E:\mitedx1\mitedx2\week1\pylab.py", line 2, in <module>
import pylab
File "E:\mitedx1\mitedx2\week1\pylab.py", line 4, in <module>
pylab.figure(1)
AttributeError: 'module' object has no attribute 'figure'
[Finished in 0.1s with exit code 1]
I tried to take help from the answers provided here
Anaconda: Unable to import pylab
but to no rescue. I have also checked the path with cmd, and it is showing the inclusion of C:\Anaconda and C:\Anaconda\Scripts there. What would be the solution to fix this problem?
You called your script pylab.py, rename it to something else.
E:\mitedx1\mitedx2\week1\pylab.py # <- you are importing from this not the pylab module
Make sure to delete the E:\mitedx1\mitedx2\week1\pylab.pyc file also.

Pandas can't find dateutil.parser after Mavericks

I wrote a python script over the summer that uses NumPy and Pandas to help me code some corpus data for the linguistics/psych lab I work in. All was fine until I upgraded to OSX Mavericks -- since then the script throws the following ImportError:
No module named dateutil.parser
Traceback (most recent call last):
File "/Users/nicholasmoores/Documents/Research/DataFrame_by_child1.9.2.py", line 30, in <module>
import pandas as pd #you will have to go through the potentially arduous process
File "/Library/Python/2.7/site-packages/pandas-0.11.0-py2.7-macosx-10.8-intel.egg/pandas/__init__.py", line 6, in <module>
from . import hashtable, tslib, lib
File "tslib.pyx", line 31, in init pandas.tslib (pandas/tslib.c:48027)
ImportError: No module named dateutil.parser
logout
which is quite frustrating! I don't know why anything else on the script would fail to work after dealing with my imports, and numpy and pandas were the only ones I was expecting to have trouble with after upgrading... FYI I'm now running Python 2.7.6
import os.path
import re
import sys
import nltk
import pickle
import numpy as np
import pandas as pd
from nltk.corpus.reader import CHILDESCorpusReader
from nltk.probability import ConditionalFreqDist, FreqDist
fd = FreqDist()
cfd = ConditionalFreqDist()
I'm sure it has to do with OSX's inhouse python getting upgraded and I suppose Pandas is trying to look for python syntax in the dateutil module that has since changed or something like that. If anyone has any suggestions they would be much appreciated as I was hoping to get a lot of data coded over the holidays!
Dateutil is a dependency of pandas, it looks like somehow this module is no longer on your system.
You'll need to (re)install dateutil:
sudo pip install dateutil
What ended up fixing the problem was redownloading and reinstalling dateutil. I'm still not completely sure where the fault was that caused Pandas to not be able to access it anymore, but I'm glad I got it all working again!

Categories