3

I'm having a problem with importing modules in python. When I run my program in the command line it works perfectly fine. However, when I try to run the same program in the python shell I am prompted with the following error:

ModuleNotFoundError: No module named 'matplotlib'

I already successfully installed matplotlib using 'python -m pip install matplotlib'. I've read this can happen when you have two different versions of python installed; however, I don't. I've uninstalled and reinstalled python and I still am having the same issue. I've also uninstalled and reinstalled matplotlib using pip.

I believe my problem is the module paths that python uses to search for imported modules are different between the two.

When I use the 'print(sys.path)' command in the python shell and the command line I get two different outputs.

Any help would be greatly appreciated!!!

The file different system paths between the python shell and the command line

1
  • This is most likely a problem with the python version of pip you are using to install the module not matching the python version you are using to run the program. Check if one of the answers below or one of the answers at stackoverflow.com/questions/37233140/python-module-not-found helps you. Commented Apr 18, 2020 at 7:59

4 Answers 4

2

You have two versions of python. I would recommend you to remove all pythons you have and go for anaconda https://www.anaconda.com/distribution/. It will fix your path problems and allow you to create environments with different versions of python. This is the least painful way also for future :) good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

Using a management/distribution system with python is a good idea, while there are others than anaconda as well.
1

I encountered this same problem – python -c "import sklearn" would work just fine, but import sklearn inside a Python program failed. Both my one-liner and program was using the same Python version (version 3.8.10).

I eventually got the program to work by replacing the shebang line (originally #!/usr/bin/python) with #!/bin/env python.

I don't know why this worked exactly (sorry). Presumably some path got reset, and the module loaded from a different location, but it might help someone so I'm posting it here nontheless. (If you know more, feel free to edit this answer.)

Comments

0

I suppose, you have both of the Python versions installed on the same computer.

If that is so, then my answer would be to go inside both Python script folders and install matplotlib on both of them.

I have also faced that issue. My path includes pip of Python 3.7.1 and whenevwer I try to import modules on Python 3.4. It throws an error!

Maybe, you could add both of the Pythons to the path.

Comments

0

Came across this exact issue yesterday. Turns out I needed to add the project to the python path. In my main callable python file I put the following before the other imports:

import sys, os
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.