0

I use Flask for the first time. The following __init__.py is working fine :

Python v3.10.6

#!/usr/bin/env python3

from flask import Flask, render_template, request
app = Flask(__name__)

@app.route('/testurl')
def testurl():
    return render_template('index.html')

@app.route('/from_client', methods=['POST'])
def from_client():
    request_data = request.get_json()
    return request_data

if __name__ == '__main__':
    app.run()

I use the following folders :

flaskApp
---- flaskApp
    ---- __init__.py
    ---- modules
        ---- mymodules.py
    ---- static
        ---- css
        ---- img
        ---- js
    ---- templates
        ---- index.html
---- flaskapp.wsgi

But when I try to change __init__.py to import mymodules from modules folder, I got "500 Internal Server Error".

The code used :

#!/usr/bin/env python3

from flask import Flask, render_template, request
from modules import mymodules
app = Flask(__name__)

@app.route('/testurl')
def testurl():
    return render_template('index.html')

@app.route('/from_client', methods=['POST'])
def from_client():
    request_data = request.get_json()
    data_id = mymodules.somecode(request_data)
    return data_id

if __name__ == '__main__':
        app.run()

I feel that there is an issue from how import work. I tried to use

import sys
#sys.path.append('[pathoftheflaskfolder/flaskApp/flaskApp/modules')

But it doesn't help either. My skill in Flask and Python are limited, so I turn around and don't find solution. If use have an idea, be my guests !

Elhjie
  • 3
  • 2
  • Welcome to Stack Overflow. [HTTP 500](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#5xx_server_errors) is a generic server-side error message. On its own it doesn't tell us anything useful. Any time you see this your first step should be to check your error logs for more detail. – ChrisGPT was on strike Apr 01 '23 at 13:15

2 Answers2

0

It looks like your code is trying to import the module whilst being within a module itself. You should instead use a relative import to make it work correctly.

from .modules import mymodules
#    ^ this dot makes it relative to the current folder
Miguel Guthridge
  • 1,444
  • 10
  • 27
-1

To create module directories, the folder will need an __init__.py, so that Python can tell it is a module, and not just a folder with code in it.

Change your file structure to the following:

flaskApp
---- flaskApp
    ---- __init__.py
    ---- modules
        ---- __init__.py  <-- NEW
        ---- mymodules.py
    ---- static
        ---- css
        ---- img
        ---- js
    ---- templates
        ---- index.html
---- flaskapp.wsgi
Miguel Guthridge
  • 1,444
  • 10
  • 27
  • This is one possible issue, but without log messages from the OP showing the actual errors and traceback, it's just a guess. – ChrisGPT was on strike Apr 01 '23 at 13:21
  • Without the `__init__.py`, their import will fail, which would cause the error they are getting if they're using a WSGI wrapper, since the flask app will never run. – Miguel Guthridge Apr 01 '23 at 13:22
  • 1
    [Not necessarily](https://stackoverflow.com/q/37139786/354577). Again. this is a _guess_. We need to see the error messages. – ChrisGPT was on strike Apr 01 '23 at 13:23
  • Oh interesting, I actually didn't know that! Thanks! – Miguel Guthridge Apr 01 '23 at 13:27
  • 1
    [It's still recommended to include an `__init__.py`](https://stackoverflow.com/a/48804718/354577), so this is good advice... it just might not be the answer to the question being asked. If OP shares their logs and this turns out to be the problem, I'll gladly turn my downvote into an upvote. – ChrisGPT was on strike Apr 01 '23 at 13:28
  • Thanks everyone ! I add an empty file `__init__.py` in the modules folder as said. Unfortunately, I got the same error. This is the log from the error.log of apache2 : `[Sun Apr 02 16:36:21.710103 2023] [wsgi:error] File "/var/www/flaskApp/flaskApp/__init__.py", line 6, in [Sun Apr 02 16:36:21.710185 2023] [wsgi:error] from modules import mymodules [Sun Apr 02 16:36:21.710293 2023] [wsgi:error] ModuleNotFoundError: No module named 'modules'` – Elhjie Apr 02 '23 at 16:59
  • @Elhjie it could also be useful to check the working directory in your setup. The import might not work correctly if you're starting in a directory that isn't `/var/www/flaskApp/flaskApp`. You could also try using a relative import - `from .modules import mymodules` – Miguel Guthridge Apr 03 '23 at 09:29
  • Thank you everyone ! It works when I used a relative import `from .modules import mymodules`. I can't really say why this will not work without the dot, but anyway it works fine ! – Elhjie Apr 04 '23 at 21:25
  • I'll write a new answer with the fix :) – Miguel Guthridge Apr 05 '23 at 06:57