Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my code throw "NameError: name 'ModuleNotFoundError' is not defined" error?

Tags:

python

I'm installing locally the following Flask app: SolarPi. Since I had to install the raven package and there are pull requests and issues unanswered on the github repo, I assumed the code to be broken and not maintained anymore. Despite of my lack of proficiency on Flask and Python I decided to give it a shot and try and get it running.

When I run

$ (.env) python manage.py server

The code throws the following error:

Traceback (most recent call last):
  File "manage.py", line 9, in <module>
    from solarpi.app import create_app
  File "/home/path/solarpi/solarpi/app.py", line 7, in <module>
    from solarpi import public, weather, charts, statistics, tables
  File "/home/path/solarpi/solarpi/public/__init__.py", line 4, in <module>
    from . import views
  File "/home/path/solarpi/solarpi/public/views.py", line 11, in <module>
    from solarpi.public.helper import get_operating_days
  File "/home/path/solarpi/solarpi/public/helper.py", line 4, in <module>
    from pysolar.util import get_sunrise_sunset
  File "/home/path/solarpi/.venv/lib/python2.7/site-packages/pysolar/__init__.py", line 1, in <module>
    from . import \
  File "/home/path/solarpi/.venv/lib/python2.7/site-packages/pysolar/radiation.py", line 21, in <module>
    from . import numeric as math
  File "/home/path/solarpi/.venv/lib/python2.7/site-packages/pysolar/numeric.py", line 140, in <module>
    except ModuleNotFoundError:
NameError: name 'ModuleNotFoundError' is not defined

As it is possible to check on file numeric.py

from math import degrees, cos, sin, radians, tan, pi
from math import acos, atan, asin, atan2, exp, e

current_mod = 'math'


def globals_import_from(module, name, name_as):
    """
    Does "from <module> import <name> as <name_as>" (globally)
    """
    ...
    ...
    Bunch of defs
    ...
    ...

try:
    import numpy
    use_numpy()
except ModuleNotFoundError:
    pass

the ModuleNotFoundError is just an exception clause.

What is tricky here is the fact that the exception "NameError", that is raised when a local or global name is not found, catches an exception thrown by another built-in exception, in this case "ModuleNotFoundError". Since those exceptions are built-in, I assumed that the code is just written to python 3 and not python 2 as stated. Am I missing anything here that would not lead to refactoring the whole code?

like image 316
pedrez Avatar asked Jan 11 '19 19:01

pedrez


Video Answer


2 Answers

Just to clarify the comments: as many said, ModuleNotFoundError is only available in Python 3.6 and later versions, but Python 2 must be used. However the ImportError exception is available in Python 2 and 3, it's just a bit less precise.

Replacing ModuleNotFoundError with ImportError should work.

like image 153
FrenchMasterSword Avatar answered Sep 24 '22 09:09

FrenchMasterSword


If you are still on Python 3.5 and run into the problem

    NameError: name 'ModuleNotFoundError' is not defined

for example when running sphinx-build or pipdeptree or from your own code, then go to PyPi and download and install the module "importlib-metada", latest version in the "2" series viz 2.1.0.

The versions 1 and 2 series of importlib-metadata include a "compat.py" file which provides the missing 'ModuleNotFoundError' functionality.

Do not use the newer/higher 3 series versions because these are for python version 3.6 or higher, and thus no longer includes the ModuleNotFoundError code in the "compat.py" file.

like image 41
Python Baby Avatar answered Sep 22 '22 09:09

Python Baby