在Railway部署Flask项目时的MySQL模块依赖错误求助
Flask项目Railway部署MySQL相关错误解决
问题背景
部署Flask项目到Railway时遇到以下系列问题:
- 安装
MySQL-python==1.2.5时触发ModuleNotFoundError: No module named 'ConfigParser',添加configparser==5.3.0后错误依旧 - 换成
mysql-connector-python后部署成功,但调用/api/login接口时出现ModuleNotFoundError: No module named 'MySQLdb'
初始构建错误日志
#11 8.037 Downloading MySQL-python-1.2.5.zip (108 kB) #11 8.071 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 108.9/108.9 KB 3.3 MB/s eta 0:00:00 #11 8.085 Preparing metadata (setup.py): started #11 8.261 Preparing metadata (setup.py): finished with status 'error' #11 8.270 error: subprocess-exited-with-error #11 8.270 #11 8.270 × python setup.py egg_info did not run successfully. #11 8.270 │ exit code: 1 #11 8.270 ╰─> [8 lines of output] #11 8.270 Traceback (most recent call last): #11 8.270 File "<string>", line 2, in <module> #11 8.270 File "<pip-setuptools-caller>", line 34, in <module> #11 8.270 File "/tmp/pip-install-sxn61bzp/mysql-python_c2900c0eabc547e1a98d6870aca796df/setup.py", line 13, in <module> #11 8.270 from setup_posix import get_config #11 8.270 File "/tmp/pip-install-sxn61bzp/mysql-python_c2900c0eabc547e1a98d6870aca796df/setup_posix.py", line 2, in <module> #11 8.270 from ConfigParser import SafeConfigParser #11 8.270 ModuleNotFoundError: No module named 'ConfigParser' #11 8.270 [end of output] #11 8.270 #11 8.270 note: This error originates from a subprocess, and is likely not a problem with pip. #11 8.273 error: metadata-generation-failed #11 8.273 #11 8.273 × Encountered error while generating package metadata. #11 8.273 ╰─> See above for output. #11 8.273 #11 8.273 note: This is an issue with the package mentioned above, not pip. #11 8.273 hint: See above for details.
添加configparser后的错误日志
#11 6.821 Collecting MySQL-python #11 6.826 Using cached MySQL-python-1.2.5.zip (108 kB) #11 6.839 Preparing metadata (setup.py): started #11 6.997 Preparing metadata (setup.py): finished with status 'error' #11 7.004 error: subprocess-exited-with-error #11 7.004 #11 7.004 × python setup.py egg_info did not run successfully. #11 7.004 │ exit code: 1 #11 7.004 ╰─> [8 lines of output] #11 7.004 Traceback (most recent call last): #11 7.004 File "<string>", line 2, in <module> #11 7.004 File "<pip-setuptools-caller>", line 34, in <module> #11 7.004 File "/tmp/pip-install-vr58ik7u/mysql-python_bc531f9d2bda4605b1f01b756a7e81cb/setup.py", line 13, in <module> #11 7.004 from setup_posix import get_config #11 7.004 File "/tmp/pip-install-vr58ik7u/mysql-python_bc531f9d2bda4605b1f01b756a7e81cb/setup_posix.py", line 2, in <module> #11 7.004 from ConfigParser import SafeConfigParser #11 7.004 ModuleNotFoundError: No module named 'ConfigParser' #11 7.004 [end of output] #11 7.004 #11 7.004 note: This error originates from a subprocess, and is likely not a problem with pip. #11 7.007 error: metadata-generation-failed #11 7.007 #11 7.007 × Encountered error while generating package metadata. #11 7.007 ╰─> See above for output.
安装mysql-connector-python后的接口错误日志
[2023-02-04 10:15:46 +0000] [1] [INFO] Starting gunicorn 20.1.0 [2023-02-04 10:15:46 +0000] [1] [INFO] Listening at: http://0.0.0.0:5456 (1) [2023-02-04 10:15:46 +0000] [1] [INFO] Using worker: sync [2023-02-04 10:15:46 +0000] [10] [INFO] Booting worker with pid: 10 [2023-02-04 10:16:36,128] ERROR in app: Exception on /api/login [POST] Traceback (most recent call last): File "/opt/venv/lib/python3.8/site-packages/sqlalchemy/util/_collections.py", line 1008, in __call__ return self.registry[key] KeyError: <greenlet.greenlet object at 0x7f3bf8ab5040 (otid=0x7f3bfa061100) current active started main> During handling of the above exception, another exception occurred: Traceback (most recent call last): File "/opt/venv/lib/python3.8/site-packages/flask/app.py", line 2525, in wsgi_app response = self.full_dispatch_request() File "/opt/venv/lib/python3.8/site-packages/flask/app.py", line 1822, in full_dispatch_request rv = self.handle_user_exception(e) File "/opt/venv/lib/python3.8/site-packages/flask_cors/extension.py", line 165, in wrapped_function return cors_after_request(app.make_response(f(*args, **kwargs))) File "/opt/venv/lib/python3.8/site-packages/flask/app.py", line 1820, in full_dispatch_request rv = self.dispatch_request() File "/opt/venv/lib/python3.8/site-packages/flask/app.py", line 1796, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) File "/app/controllers/login.py", line 11, in login user_exist = Users.query.filter_by(uname=user).first() File "/opt/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 552, in __get__ return type.query_class(mapper, session=self.sa.session()) File "/opt/venv/lib/python3.8/site-packages/sqlalchemy/orm/scoping.py", line 47, in __call__ sess = self.registry() File "/opt/venv/lib/python3.8/site-packages/sqlalchemy/util/_collections.py", line 1010, in __call__ return self.registry.setdefault(key, self.createfunc()) File "/opt/venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py", line 4228, in __call__ return self.class_(**local_kw) File "/opt/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 176, in __init__ bind = options.pop('bind', None) or db.engine File "/opt/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 998, in engine return self.get_engine() File "/opt/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1017, in get_engine return connector.get_engine() File "/opt/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 594, in get_engine self._engine = rv = self._sa.create_engine(sa_url, options) File "/opt/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 1027, in create_engine return sqlalchemy.create_engine(sa_url, **engine_opts) File "<string>", line 2, in create_engine File "/opt/venv/lib/python3.8/site-packages/sqlalchemy/util/deprecations.py", line 309, in warned return fn(*args, **kwargs) File "/opt/venv/lib/python3.8/site-packages/sqlalchemy/engine/create.py", line 548, in create_engine dbapi = dialect_cls.dbapi(**dbapi_args) File "/opt/venv/lib/python3.8/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 163, in dbapi return __import__("MySQLdb") ModuleNotFoundError: No module named 'MySQLdb'
错误原因
MySQL-python是仅支持Python2的老旧包,Python3中ConfigParser模块已重命名为configparser,且该包早已停止维护,因此即使安装configparser也无法兼容其导入逻辑。- 改用
mysql-connector-python后,SQLAlchemy默认尝试使用MySQLdb驱动连接数据库,但当前环境未安装该驱动,导致接口调用失败。
解决方案
方案一:改用PyMySQL(推荐,Python3主流MySQL驱动)
- 修改
requirements.txt,删除MySQL-python和mysql-connector-python,添加:
PyMySQL==1.1.0
- 更新Flask项目的数据库连接URL,将原URL前缀从
mysql://改为mysql+pymysql://,示例:
# 原配置 SQLALCHEMY_DATABASE_URI = "mysql://username:password@host:port/dbname" # 修改后 SQLALCHEMY_DATABASE_URI = "mysql+pymysql://username:password@host:port/dbname"
方案二:继续使用mysql-connector-python
- 确保
requirements.txt中保留mysql-connector-python:
mysql-connector-python==8.2.0
- 修改数据库连接URL前缀为
mysql+mysqlconnector://,示例:
SQLALCHEMY_DATABASE_URI = "mysql+mysqlconnector://username:password@host:port/dbname"
完成上述修改后,重新部署项目到Railway即可解决所有问题。
内容的提问来源于stack exchange,提问作者julcodecode
相关产品推荐
相关产品推荐

