为什么我的 python 代码会出现内部服务器错误?

Why am I getting internal server error with my python code?

我正在 Flask 网页上进行实验和工作,它将使用 MySQL 数据库中的一些数据,经过一些编码后我遇到了内部服务器错误,我不知道为什么。

几天后我无法解决问题,我该怎么做才能让它工作?

我正在使用 Python3,托管的服务器是 Ubuntu 系统,我现在可以使用我托管的服务器 LAN 域(服务器名称)名称通过 LAN 访问该站点通过 Hyper-V,因为我没有备用 PC,上面有 Ubuntu。

奇怪的是,当我在我的笔记本电脑上进行本地测试时,只有当我想连接托管在服务器虚拟机上的页面时,我才会收到 500 错误。

因为我不知道问题在哪里我上传了大部分文件到GitHub,这当然会被更改,后来出于安全原因被删除,但现在更容易了。

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

这是 apache2 错误日志

[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/

这是来自 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

不要下载我误content_management为pip模块的模块。你可以卸载模块 pip uninstall contentful_management

错误出现是因为你在init.py.

中导入模块

试试这个:

from name_of_the_parent_directory import content_management 

你试试用

from . import content_management

name_of_the_parent_directory - 表示父目录,在您的 github 中它将是网页。