12

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?

6
  • Python 3.x and Python 2.7 are essentially two different languages. 3.x has ModuleNotFoundError. 2.7 does not. You are using 2.7.
    – DYZ
    Commented Jan 11, 2019 at 19:46
  • Yep, I get that. From the instructions the coder asks us to use python2 but on his code he uses ModuleNotFoundError which is from python3. Am I suppose to rewrite the code to fallback to the previous version? If yes, do you know how would I do that?
    – pedrez
    Commented Jan 11, 2019 at 19:48
  • If ModuleNotFoundError is the only problem, I guess you can create a new exception class with that name?
    – DYZ
    Commented Jan 11, 2019 at 19:51
  • 2
    Can't you use ImportError instead? It is python2 compatible. Commented Jan 11, 2019 at 19:51
  • I've skimmed through the source code and I'm not seeing anything that won't run in Python 3
    – roganjosh
    Commented Jan 11, 2019 at 19:54

3 Answers 3

14

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.

1
  • 3
    This answer should be corrected; ModuleNotFoundError is available from Python3.6 onwards. Commented Sep 16, 2019 at 8:28
2

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.

1

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.

5
  • I tried this solution and it cries python3 manage.py server Traceback (most recent call last): File "manage.py", line 6, in <module> from flask_script import Manager, Shell, Server ModuleNotFoundError: No module named 'flask_script' . Doesn't work on my end.
    – pedrez
    Commented Jan 11, 2019 at 20:10
  • This is just so confusing! Here I can see the ModuleNotFoundError which makes sense since I ran python3. But still confusing!
    – pedrez
    Commented Jan 11, 2019 at 20:15
  • 1
    It looks like you create a virtualenv for your flask script, which contains all the modules, and a python2 instance. When you run it with python3, it can't find any of the modules inside your virtualenv. Easiest option is probably to create a new python3 virtualenv, install everything in there, and retry. Commented Jan 11, 2019 at 21:49
  • This piece of advice might be useful in a near future. Thanks for sharing!7
    – pedrez
    Commented Jan 12, 2019 at 15:44
  • I don't think it's python 2/3 matter, but python <3.6 matter. Commented Sep 15, 2019 at 8:12

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.