Python Forum
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask cannot see my modules
#1
I have a Flask application and each python module has a doctest which runs successfully. However, when I run the modules under flask, I get "module not found."

Main code (__init.py__)
import os
import functools
from .make_plot import make_plot
from .get_counties import get_counties, get_states

from flask import Flask, render_template, redirect, url_for, request, session, flash, g
#from flask_debugtoolbar import DebugToolbarExtension
...
The make_plot module:
import pandas as pd
import datetime as dt
import numpy as np
import sys

import configparser

from load_data import load_data
from plots import *
from data_preprocessing import pre_processing, get_diffs, join_state, join_days, normalize, join_county
from preprocess_us import preprocess_us
The error traceback:
flask.cli.NoAppException: While importing "web-plots", an ImportError was raised:

Error:
Traceback (most recent call last): File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app __import__(module_name) File "/home/anna_user2/data-science-book/covid-19/web-plots/__init__.py", line 3, in <module> from .make_plot import make_plot File "/home/anna_user2/data-science-book/covid-19/web-plots/make_plot.py", line 8, in <module> from load_data import load_data ModuleNotFoundError: No module named 'load_data'
Adding a period to the import statement, as below, does not help and it breaks my unit tests
from .load_data import load_data
Reply
#2
We'd love to help, but we need you to help us help you. Maybe someone smarter than me will answer this without these details, but what I'm looking for is a minimal (smallest amount of code possible) and full (I can reproduce the problem locally) example; currently, you've left details out that make this difficult to answer.

When I say minimal, I include meaning that there shouldn't be any third-party module imports unless they're essential for reproducing the problem. And when I say full, I mean if you leave out a file that is essential, it's going to make it harder for us to help.
Reply
#3
OK I have removed the other modules. What is left: the main in __init__.py, which has only
from flask import Flask
from .make_plot import make_plot

def create_app():
    app = Flask(__name__)
    app.config.from_mapping(SECRET_KEY='dev')

    app.config.from_pyfile('config.py', silent=True)
    return app

app=create_app()

@app.route("/covid-plots")
def home():
    return "hello world"
load_data has
from load_data import load_data
That is all there is. The traceback looks the same, i.e.
flask.cli.NoAppException
flask.cli.NoAppException: While importing "minimal", an ImportError was raised:

Traceback (most recent call last):
File "/home/anna_user2/.venv/projects/web-plots/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
__import__(module_name)
File "/home/anna_user2/data-science-book/covid-19/minimal/__init__.py", line 1, in <module>
from .make_plot import make_plot
File "/home/anna_user2/data-science-book/covid-19/minimal/make_plot.py", line 1, in <module>
from load_data import load_data
ModuleNotFoundError: No module named 'load_data'
Reply
#4
You need to be painfully specific about how to reproduce the problem. The way you've described it, it sounds like load_data.py tries to import itself, and you haven't said what you're doing to run the code. Also, if you can reproduce this without the Flask import, that would be ideal, but of course keep it if it's actually required.
Reply
#5
The problem only occurs with Flask. The only missing information about this "minimal" configuratioon that I can think of is where the modules are: everything is in the directory called "miminal" (__init__.py, make_plot.py and load_data.py).

I set the environmental variables with the statements:
echo 'Be sure to "source" this script rather than simply running it'
export FLASK_ENV=development
export FLASK_APP='.'

I run the code with via the comnand 'flask run --host=0.0.0.0 --port=3000'


The reason it looks like load_data tries to import itself is that load_data is both the name of the module (the name of the py file) and the name of the function within the module. load_data.py does not contain any import statements. Here is the code:

import os

def load_data (download = False):
    pass

I did some further research, displaying the contents of sys.path. When I execute __init__.py from the command line the path includes the directory "minimal" where the module is. When I run it under Flask, the directory "minimal" is not there.

Can this thread be moved to the "flask" forum?

The solution seems to be

export PYTHONPATH='.'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Modules issue, pip3 download modules only to pyhton3.5.2 not the latest 3.6.1 bmohanraj91 6 8,427 May-25-2017, 08:15 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020