Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: "ModuleNotFoundError", but module is installed?

I realize this seems like a generic question, but all answers pointed to having two simultanious python installations - I already uninstalled the other one.

Currently I run my code from PyCharm 2017.1.5 (windows 10) with Python interpreter set as Python 3.6.1 (C:\Anaconda3\python.exe), i.e. I installed Anaconda3, which includes the matplotlib, and run from PyCharm using the Ananconda3-interpreter.

I've checked in Anaconda Navigator that matplotlib 2.0.2 is installed in the environment.

A minimal (non-working) example:

import matplotlib.pyplot as plt

plt.plot(range(10))
plt.show()

Returns the following error:

C:\Anaconda3\python.exe C:/Users/John/Documents/CPU/master/untitled/main11.py
Traceback (most recent call last):
  File "C:/Users/John/Documents/CPU/master/untitled/main11.py", line 1, in <module>
    import matplotlib.pyplot as plt
  File "C:\Anaconda3\lib\site-packages\matplotlib\pyplot.py", line 29, in <module>
    import matplotlib.colorbar
  File "C:\Anaconda3\lib\site-packages\matplotlib\colorbar.py", line 34, in <module>
    import matplotlib.collections as collections
  File "C:\Anaconda3\lib\site-packages\matplotlib\collections.py", line 37, in <module>
    import matplotlib.lines as mlines
  File "C:\Anaconda3\lib\site-packages\matplotlib\lines.py", line 28, in <module>
    from matplotlib.markers import MarkerStyle
ModuleNotFoundError: No module named 'matplotlib.markers'

Process finished with exit code 1

This ran fine 2 weeks ago, but not now. To my knowledge, I didn't change or update anything. The module loads correctly, but it seems to be a change in the module content? If so: How did that happen and how can I fix it?

like image 527
RasmusP_963 Avatar asked Aug 15 '17 13:08

RasmusP_963


3 Answers

@ImportanceOfBeingErnest lead me in the right direction. I post my solution here s.t. others may find the answer. The problem was a corrupted disk sector - an unlikely event of chance.

The problem was indeed in the matplotlib-package itself. Retrospectively, pointers to the issue were that errors in pre-distributed packages should not exist. If they do, external circumstances must have corrupted and the problem is not with the Python-installation itself.

I uninstalled matplotlib through Anaconda Prompt with conda remove matplotlib and re-installed with conda install matplotlib. This gave me this error:

(C:\Anaconda3) C:\Users\John>conda install matplotlib
[...]
ERROR conda.core.link:_execute_actions(337): An error occurred while installing package 'defaults::matplotlib-2.0.2-np112py36_0'.
OSError(22, 'Invalid argument') Attempting to roll back. 

OSError(22, 'Invalid argument')

Before @Ernest's comment, I thought it maybe had to do with non-ASCII in PATH or similar.

Instead I tried to reinstall Anaconda3 completely, restarted and found that part of the Anaconda3-folder weren't removed (the one containing the matplotlib).

Deleting it manually gave a Windows error 0x80070570. Following this post on ServerFault (the comment to OP) I ran a check and afterwards a repair from Windows Explorer GUI: Right-click on the drive in This PC --> Properties --> Tab Tools --> Check (repair appears if any errors are found).

After some restarts, reinstalling Anaconda3 from scratch and restarting again, I was able to run my project again!

like image 95
RasmusP_963 Avatar answered Nov 13 '22 02:11

RasmusP_963


In my case, I could fix it by setting PYTHONPATH to the path to site-packages folder where the needed packages are located, excluding site-pacages.

I use a pyenv virtual environment, whose path is /home/apk/.pyenv/versions/python-3-7-4. When the environment is activated, pip installs packages to /home/apk/.pyenv/versions/python-3-7-4/lib/python3.7/site-packages. Therefore, in terminal, I set:

$ PYTHONPATH=/home/apk/.pyenv/versions/python-3-7-4/lib/python3.7/

The same should be true for Windows installations of python. If virtual environmets are used, then one could edit activate script to set PYTHONPATH.

After I did it, I checked in a python shell.

$ python
Python 3.7.4 (default, Feb  5 2020, 17:11:33) 
[GCC 5.5.0 20171010] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/home/apk/.pyenv/versions/3.7.4/lib/python37.zip', '/home/apk/.pyenv/versions/3.7.4/lib/python3.7', '/home/apk/.pyenv/versions/3.7.4/lib/python3.7/lib-dynload', '/home/apk/.pyenv/versions/python-3-7-4/lib/python3.7']
>>> sys.executable
'/home/apk/.pyenv/versions/python-3-7-4/bin/python'

Good luck!

References

  • https://github.community/t5/Programming-Help-and-Discussion/Python-ModuleNotFoundError-although-module-is-installed-aiohttp/td-p/28525
like image 4
antonini44354 Avatar answered Nov 13 '22 03:11

antonini44354


It is difficult to answer this question directly, however, I have seen a large amount of issues in corporate Windows environments with PyCharm and Anaconda these are some of the issues you may be having

  1. Check you PATH is correctly pointing to all Anaconda locations

    import sys
    sys.path
    
  2. Check that your files have not been migrated to C:\Users\username\AppData\Roaming by your IT team

  3. Purge your system for any python distributions. There may be software distributions that you use internally that package their own python distribution. This can often be included in the PATH. Another example could be installing Anaconda to your C:\ but also having it already installed in Users\Local\AppData or 'C:\Program Files' months before and forgotten!

A good way of directly debugging your problem would be to navigate to the following directory in ipython

C:\Anaconda3\lib\site-packages\matplotlib

and they try import matplotlib.markers

If this fails then you could then try

import matplotlib
matplotlib.__file__

you should check that this result gives

'C:\\Anaconda3\\lib\\site-packages\\matplotlib\\__init__.pyc'

as most likely there will be another matplotlib version installed that is taking precedence. This will then fall under one of the issues above to correct.

like image 1
Alexander McFarlane Avatar answered Nov 13 '22 02:11

Alexander McFarlane