0

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:

  1. Based on the info in this answer: Python Database connection for mariadb using sqlalchemy I tried using mysql+pymysql instead of mariadb+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’
  1. 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’
  1. 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'
8
  • 2
    can you post more of the error message? Commented Dec 6, 2024 at 16:01
  • 1
    @python_user added the full error message to the question, thanks! Commented Dec 6, 2024 at 16:28
  • 1
    Have you actually installed pymysql/mariadb-connector etc with pip? Also, is this code running in Google Cloud (or a local emulation of it)? Commented Dec 6, 2024 at 19:20
  • 1
    @snakecharmerb -- yep, I've installed both with pip. The code is running on a remote linux machine, unrelated to google cloud. Commented Dec 6, 2024 at 19:48
  • 1
    If you open a python interpreter in your venv (or whatever) and do import pymysql, what does pymysql.__file__ show? Is it what you expect? Commented Dec 6, 2024 at 20:06

1 Answer 1

0

It was a permissions issue, in my venv directory, some libraries were installed by the user that runs the program (runcloud), and others were installed by root. For example:

drwxrwxr-x+  3 runcloud runcloud   4096 Nov 22 17:02 jinja2
drwxrwxr-x+  3 runcloud runcloud   4096 Nov 22 17:02 jiter
drwxr-xr-x+  4 root     root       4096 Dec  5 20:21 mariadb
drwxrwxr-x+  3 runcloud runcloud   4096 Nov 22 17:02 markupsafe
drwxr-xr-x+ 24 root     root       4096 Dec  5 19:06 numpy

This allowed the python program to recognize the folders (and import the libraries) but it couldn't access anything inside of them. Hence the missing attribute error.

To fix the issue I went through each library installed by root, and copied the permissions and ownership from one of the functional libraries (flask).

Here's an example for mariadb:

chmod -R --reference=flask mariadb
chown -R --reference=flask mariadb

After this the program could import the libraries and access the files inside without any issues.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.