I'm trying to connect to a mariadb database using Flask SQLAlchemy but I am getting the following error:
AttributeError: module 'mariadb' has no attribute 'paramstyle'
Here's the function that configures the URI and attempts the connection:
def create_app():
# APP SETUP
app = Flask(__name__, instance_relative_config=True)
app.config.from_mapping(
SQLALCHEMY_DATABASE_URI='mariadb+mariadbconnector://user:pass@localhost:post/database',
)
# DATABASE SETUP
db.init_app(app)
with app.app_context():
db.create_all()
return app
So far I've done the following to attempt to solve the issue:
- Based on the info in this answer: Python Database connection for mariadb using sqlalchemy I tried using
mysql+pymysqlinstead ofmariadb+mariadbconnector, but all that resulted in was a new source for the missing attribute. Running my script would output this error:
AttributeError: module 'pymysql' has no attribute 'paramstyle’
- My second attempt, following a separate answer in the same thread, I tried switching to
mysql+mysqldb, but that once again changed the source of the error to MySQLdb:
AttributeError: module 'MySQLdb' has no attribute 'paramstyle’
- I switched back to MariaDB, and following this documentation: https://mariadb.com/docs/server/connect/programming-languages/c/install/#CS_Package_Repository, installed the following packages:
sudo apt install libmariadb3 libmariadb-dev
But still no luck, I'm getting the same error as when I started: AttributeError: module 'mariadb' has no attribute 'paramstyle'
Any more ideas as to why in every library this paramstyle attribute is missing? I assume this is some kind of error related to Flask SQLAlchemy since it's the common denominator, but I haven't been able to find anything.
Thanks!
Here's the full error message:
Traceback (most recent call last):
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/arbiter.py", line 608, in spawn_worker
worker.init_process()
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 135, in init_process
self.load_wsgi()
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/workers/base.py", line 147, in load_wsgi
self.wsgi = self.app.wsgi()
^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/app/base.py", line 66, in wsgi
self.callable = self.load()
^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 57, in load
return self.load_wsgiapp()
^^^^^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/app/wsgiapp.py", line 47, in load_wsgiapp
return util.import_app(self.app_uri)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/gunicorn/util.py", line 370, in import_app
mod = importlib.import_module(module)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "/home/runcloud/webapps/spotlessmind-app/wsgi.py", line 3, in <module>
app = create_app()
^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/spotlessmind/__init__.py", line 28, in create_app
db.init_app(app)
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/flask_sqlalchemy/extension.py", line 374, in init_app
engines[key] = self._make_engine(key, options, app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/flask_sqlalchemy/extension.py", line 665, in _make_engine
return sa.engine_from_config(options, prefix="")
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 820, in engine_from_config
return create_engine(url, **options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 2, in create_engine
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/engine/create.py", line 612, in create_engine
dialect = dialect_cls(**dialect_args)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/dialects/mysql/mariadbconnector.py", line 143, in __init__
super().__init__(**kwargs)
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/dialects/mysql/base.py", line 2518, in __init__
default.DefaultDialect.__init__(self, **kwargs)
File "<string>", line 2, in __init__
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/util/deprecations.py", line 281, in warned
return fn(*args, **kwargs) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^
File "/home/runcloud/webapps/spotlessmind-app/venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 333, in __init__
self.paramstyle = self.dbapi.paramstyle
^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'mariadb' has no attribute 'paramstyle'
pip? Also, is this code running in Google Cloud (or a local emulation of it)?import pymysql, what doespymysql.__file__show? Is it what you expect?