简体   繁体   中英

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

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?

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.

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.

The problem isn't a missing module, it's a missing exception class. The following code:

try:
    import numpy
    use_numpy()
except ModuleNotFoundError:
    pass

Is supposed to try to import numpy , but if it fails, just to ignore that failure and continue ( pass = do nothing).

However, the code is trying to catch the exception ModuleNotFoundError - which doesn't exist in python2.7.

Just run your code with python3 instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM