0

I'm experimenting and working on a Flask webpage what's gonna use some data from MySQL database and after a little coding I got internal server error and I don't know why.

After several days I cant work out the problem what can I do for it to work?

I'm using Python3, the server where is hosted is Ubuntu system and I can reach the site via LAN right now with the servers LAN domain (server's name) name that I host via Hyper-V cause I don't have a spare PC for that with Ubuntu on it.

The weird part abut it that when I test it locally on my laptop I wont get the 500 Error only if I want to connect the page what's hosted on the server virtual machine.

Because I don't know where is the problem I uploaded most of the files to GitHub, this gonna be changed of course and later deleted for security reasons but now its easier.

Link: https://github.com/Csabatron99/Webpage

Here is the apache2 error log

[Wed Jul 07 12:42:08.597500 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] mod_wsgi (pid=10179): Failed to exec Python script file '/var/www/FlaskApp/flaskapp.wsgi'.
[Wed Jul 07 12:42:08.597550 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] mod_wsgi (pid=10179): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'.
[Wed Jul 07 12:42:08.597762 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] Traceback (most recent call last):
[Wed Jul 07 12:42:08.597784 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170]   File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>
[Wed Jul 07 12:42:08.597788 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170]     from FlaskApp import app as application
[Wed Jul 07 12:42:08.597793 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 11, in <module>
[Wed Jul 07 12:42:08.597795 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170]     from .dbconnect import connection_db
[Wed Jul 07 12:42:08.597799 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170]   File "/var/www/FlaskApp/FlaskApp/dbconnect.py", line 3, in <module>
[Wed Jul 07 12:42:08.597802 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170]     import mysql.connector
[Wed Jul 07 12:42:08.597811 2021] [wsgi:error] [pid 10179] [client fe80::1047:5664:1d7b:6d86:1170] ModuleNotFoundError: No module named 'mysql'
[Wed Jul 07 12:42:08.816124 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] mod_wsgi (pid=10178): Failed to exec Python script file '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816173 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] mod_wsgi (pid=10178): Exception occurred processing WSGI script '/var/www/FlaskApp/flaskapp.wsgi'., referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816386 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] Traceback (most recent call last):, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816409 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171]   File "/var/www/FlaskApp/flaskapp.wsgi", line 7, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816413 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171]     from FlaskApp import app as application, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816418 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171]   File "/var/www/FlaskApp/FlaskApp/__init__.py", line 11, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816426 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171]     from .dbconnect import connection_db, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816431 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171]   File "/var/www/FlaskApp/FlaskApp/dbconnect.py", line 3, in <module>, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816433 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171]     import mysql.connector, referer: http://tikva-server-hv/
[Wed Jul 07 12:42:08.816443 2021] [wsgi:error] [pid 10178] [client fe80::1047:5664:1d7b:6d86:1171] ModuleNotFoundError: No module named 'mysql', referer: http://tikva-server-hv/

and this is the code from the dbconnect.py

import mysql.connector

def connection_db():
    conn1 = connector.connect(host="localhost",
                                    user = "root",
                                    passwd = "pass",
                                    db = "db")
    c1 = conn1.cursor()
    return c1, conn1
Csabatron99
  • 15
  • 1
  • 5
  • you can see in the logs where your server is running or run the app using app.run(debug=True). Please don't give link to you repo making people read through it rather provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Also read the [how to ask](https://stackoverflow.com/help/how-to-ask). – charchit Jul 07 '21 at 09:56
  • Ok one sec I will look up for the log. In the repo is just an experiment code not the actual code I'm going to use so I don't think there is anything that gonna be a problem. And for the how to ask section I'm not using this site very often so sorry if I asked the question wrongly or something. – Csabatron99 Jul 07 '21 at 10:03
  • The apache 2 log say's that in the .wsgi file this line causes the problem 'from FlaskApp import app as application' – Csabatron99 Jul 07 '21 at 10:07
  • Please [edit](https://stackoverflow.com/posts/68283691/edit) your question and paste the logs there for better understanding. and as well we don't see any wsgi file in the github repo. so we can't help out. – charchit Jul 07 '21 at 10:10
  • Done and I uploaded here the wsgi – Csabatron99 Jul 07 '21 at 10:16
  • the error says no module named content_management. please verify if the module is installed. you can reinstall it. `pip install contentful_management` – charchit Jul 07 '21 at 10:18
  • The content_management is a python file in the same directory where is the init file. For duble check reasons I installed the module you mentioned but didnt resolve the issue – Csabatron99 Jul 07 '21 at 10:30

1 Answers1

0

Do not download the module I mistook content_management as pip module. you can uninstall the module pip uninstall contentful_management

The error comes because you are importing module in init.py.

try this:

from name_of_the_parent_directory import content_management 

You an try using

from . import content_management

name_of_the_parent_directory - means the parent directory, in your github it will be webpage.

charchit
  • 1,492
  • 2
  • 6
  • 17
  • On the server the __init__.py and the content_management.py is in the same directory so I don't really understand it what I need to do. By the way I tried to it inserted the name of the directory and then the content_management but didn't resolve the issue – Csabatron99 Jul 07 '21 at 11:10
  • And based on what I found to import a python file module you just need this code: import – Csabatron99 Jul 07 '21 at 11:38
  • When importing local python modules to __init__.py you simply can't do import filename . I edited the answer, you can have a look. As to why I clearly don't know but you can have look at similar question. https://stackoverflow.com/questions/34753206/init-py-cant-find-local-modules – charchit Jul 07 '21 at 12:01
  • So update: with the link what you send its working now. But now it has an error with the database connector. I use this > https://github.com/mysql/mysql-connector-python < and after i installed the module and used the import code like in the little example it's says there is no kind of module. – Csabatron99 Jul 07 '21 at 12:27
  • Can you be more specific with the error. What kind of module does the error says dbconnect or mysql-connector. Please paste the logs again by editing it. I maybe sounding rude but I can't help you without knowing the correct error. – charchit Jul 07 '21 at 12:42
  • Yeah sorry don't worry I was the dumb dumb who didn't updated the log :D tried to reinstall the module with pip and searching for solution but i did not get lucky with it . – Csabatron99 Jul 07 '21 at 12:47
  • 8s there any file mysql because the code that was before in this file was the right code. Try the previous code written in this file. The one which is in the github repo – charchit Jul 07 '21 at 12:59
  • Sorry it is that code there was another file and accidentally pasted that code I updated it. This code wont work the previous one was an attempt to fix it. – Csabatron99 Jul 07 '21 at 13:07
  • Still the same errro? You have to use `mysql.connector.connect` not connector.connect in dbconnect.py – charchit Jul 07 '21 at 13:11
  • yep still the same error and with the `mysql.connector.connect` too Still `ModuleNotFoundError: No module named 'mysql'` – Csabatron99 Jul 07 '21 at 13:18
  • `pip3 install mysql-connector-python-rf` try this, what is the version of Ubuntu. And open a terminal type `python` hit after and execute import mysql.connector . See if it is imported without any error. – charchit Jul 07 '21 at 13:30
  • The package was already installed and in python it didn't give back the error just in the apache error log and the version is 20.04 – Csabatron99 Jul 07 '21 at 13:33
  • How are you installing modules in your machine, if not using sudo, then try using `sudo pip3 install mysql-connector-python-rf` – charchit Jul 07 '21 at 13:42
  • oh my god :D with sudo permission it installed correctly or what ? I saw that with sudo its gonna conflict sometimes or be broken permissions but now its working properly. Thank you very much. – Csabatron99 Jul 07 '21 at 13:45