Help connecting Python to MariaDB on a Mac

I’ve installed Python 3.14.3 from the Python website and it works. I’ve installed – pip install mariadb – and the install was successful with Terminal saying it is installed. However, when I enter import mariadb in Python, I get an error message saying the module does not exist.

I discovered the mariadb module is located in ‘‘‘/opt/anaconda3/lib/python3.13/site-packages/mariadb‘‘‘ but I don’t use Anaconda.

Where do I need to move these files so Python can see them? I really need to connect to this database.

Thanks,

Larry

You could try to add the path you have to the PYTHONPATH environmental variable:

export PYTHONPATH='/opt/anaconda3':${PYTHONPATH}

Install it again using python -m pip, and it should install into your 3.14.

You either had a conda env activated, or the conda pip appears first on $PATH and installed mariadb into its 3.13 instead

sji: You give me too much credit for what little I know. I’m just a simple new user. Um, where do I add this statement? (Path and file?)

James, thanks for your reply!!

In Terminal, I typed: python –m pip install mariadb

And this message appeared:

Requirement already satisfied: mariadb in /opt/anaconda3/lib/python3.13/site-packages (1.1.14)

Requirement already satisfied: packaging in /opt/anaconda3/lib/python3.13/site-packages (from mariadb) (25.0)

Thoughts?

Larry

You can simply paste the command I provided earlier in your terminal.
Note that @JamesParrott’s answer is spot on.

You can:

  1. Do simply
conda deactivate

and redo what James suggested you.

  1. follow sji suggestion if you want to use anaconda. That you have, even if you think you don’t.

  2. Or you can uninstall it and use uv. I kindly suggest that:

conda deactivate
/opt/anaconda3/uninstall.sh --remove-caches
python3 -m pip install uv
uv init put_here_the_app_name
cd put_here_the_app_name
uv add mariadb

If uv add fails, try (ubuntu):

sudo apt install libmariadb3 libmariadb-dev
uv add mariadb

To check if it worked:

. .venv/bin/activate
python -m mariadb

It should say something like 'mariadb' is a package and cannot be directly executed

Lucas: I’m almost there - confused on one point

I deactivated conda

I removed anaconda caches

I installed python 3 - the uv instruction worked

Pip then told me it needed to upgrade to 26.0.1 - so I did that successfully.

Your next instruction confused me - uv init put_here_the_app_name

I’m not that bright. Which app name - python, mariadb, or something else?
And is that the same app name I should use in the cd statement?

So far, your instructions worked great - then I hit the wall of my ignorance.

Thanks!

whatever you want :slight_smile:

my guess! is that since I’ve already installed Python, I don’t need to do that again. So I would write: uv init mariadb ??
then cd mariadb ??

if so, then why am I writing uv add mariadb?

Larry

It’s only a name. Name it, for example uv init mariadb_test.

Try it. If it works, it works :slight_smile:

OK, getting closer.

uv init mariadb_test worked
cd mariadb_test switch directories
uv add mariadb worked

..venv/bin/activate did NOT work - “no such files or directory”

The Terminal prompt now reads: …Mac-Studio mariadb_test %

I’m not sure what I did, so I’m not sure if i’m good to go

Larry

It’s not double dots, there’s a space between them:

. .venv/bin/activate

you can also write

source .venv/bin/activate

the single dot is an hacky shortcut :stuck_out_tongue:

I found the MariaDB folder - its in my user directory.
I assume ? I can delete this.
My bigger question is whether I’ve reset the system enough that I can install mariadb?

Ah! This time, no error message.

Um, what have I done?

You activated a venv.

OK - I entered: python -m mariadb and got this:

SyntaxWarning: “\*” is an invalid escape sequence. Such sequences will not work in the future. Did you mean “\\*”? A raw string is also an option.

/Users/larryjordan/mariadb_test/.venv/bin/python: No module named mariadb._main_; ‘mariadb’ is a package and cannot be directly executed

Done! Now you can create your script and import mariadb and use it.

You can activate the venv every time and execute the script or just write

uv run your_script_name.py

Great! I’ve never worked with a virtual environment before - something new to learn.

If I wanted to install mariadb into my “main” Python installation, do I need to issue a different pip command?

Last question, I hope. If I want to install a library ONLY into the vent, which pip command do I use?

Sorry to be so new. I’m deeply grateful for your help.

Larry

Ok some explaining:

# it installs uv. Something like pip, but with steroids
python3 -m pip install uv
# it creates a new project, with a venv and bells and whistles
uv init put_here_the_app_name
cd put_here_the_app_name
# install mariadb and add it as a project dep
uv add mariadb

To know more: