2

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

Fletch22
  • 31
  • 3
  • 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 https://stackoverflow.com/questions/37233140/python-module-not-found helps you. – Cibin Joseph Apr 18 '20 at 07:59

3 Answers3

1

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.

Prefect
  • 1,719
  • 1
  • 7
  • 16
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.

Jaidee
  • 31
  • 3
0

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

zrajm
  • 1,361
  • 1
  • 12
  • 21