Working with an Access database in Python on non-Windows platform (Linux or Mac)

Posted on

Question :

Working with an Access database in Python on non-Windows platform (Linux or Mac)

I want to access the data in a Microsoft Access database. I have some .accdb and .mdb files and want to read them in Python.

From my research, pyodbc can only be used on Windows platform, but I am working on Mac OS X.
I am new to Python.

The other option is if I could export the data from the database to a csv and then use in python.

Any help or starting would be highly appreciated.

Answer #1:

“From my research, pyodbc can only be used on Windows platform”

Not true. The main pyodbc page says

Precompiled binary wheels are provided for most Python versions on Windows and macOS. On other operating systems [pip install pyodbc] will build from source.

However, it is certainly true that using ODBC to manipulate an Access database is mainly done on Windows. “MDB Tools”, along with “unixODBC”, is often mentioned as a way to work with Access databases on non-Windows platforms, but in my limited experience I have found that it really just doesn’t work very well (when it works at all).

Of course, you can always purchase a third-party MS Access ODBC driver for your non-Windows platform, but if you want a free open-source solution you can use the UCanAccess JDBC driver. There are two ways to accomplish that: JayDeBeApi, and Jython.

In both cases you will need to download the latest version of UCanAccess (available for download here) and unpack the “” file to a convenient location, making sure to preserve the folder structure:

UCanAccess folder

(In the following examples I unpacked it to ~/Downloads/JDBC/UCanAccess.)


Option 1: JayDeBeApi

This is the preferred option since it should work with your existing Python setup. You can install JayDeBeApi with pip.

If you don’t already have a JRE (Java Runtime Environment) installed then you’ll need that, too. (I used sudo apt install default-jre on Ubuntu.)

Once the required components are in place you should be able to use code like this:

import jaydebeapi

db_path = "/home/gord/test.accdb"

ucanaccess_jars = [
classpath = ":".join(ucanaccess_jars)
cnxn = jaydebeapi.connect(
    ["", ""],
crsr = cnxn.cursor()
    crsr.execute("DROP TABLE table1")
except jaydebeapi.DatabaseError as de:
    if "user lacks privilege or object not found: TABLE1" in str(de):
crsr.execute("CREATE TABLE table1 (id COUNTER PRIMARY KEY, fname TEXT(50))")
crsr.execute("INSERT INTO table1 (fname) VALUES ('Gord')")
crsr.execute("SELECT * FROM table1")
for row in crsr.fetchall():


Option 2: Jython

(Note that Jython is a separate implementation of Python, it only supports Python 2.7, and is apparently no longer under active development.)

Important: The following instructions are for UCanAccess version 3.0.5 or later.

After …

  • installing Jython (via sudo apt-get install jython on Ubuntu) and
  • downloading UCanAccess and unpacking it as described above

I created the following Jython script named “”

from com.ziclix.python.sql import zxJDBC

jdbc_url = "jdbc:ucanaccess:///home/gord/Documents/test.accdb"
username = ""
password = ""
driver_class = "net.ucanaccess.jdbc.UcanloadDriver"

cnxn = zxJDBC.connect(jdbc_url, username, password, driver_class)
crsr = cnxn.cursor()
crsr.execute("SELECT AgentName FROM Agents")
for row in crsr.fetchall():
    print row[0]

and ran it with the following shell script

export CLASSPATH=.:/home/gord/Downloads/JDBC/UCanAccess/loader/ucanload.jar
Answered By: Gord Thompson

Answer #2:

On Mac OSx and Ubuntu 18.04 you can use pandas_access

From the documentation:

import pandas_access as mdb

db_filename = 'my_db.mdb'

# Listing the tables.
for tbl in mdb.list_tables(db_filename):

# Read a small table.
df = mdb.read_table(db_filename, "MyTable")

On Ubuntu you may need to run:

sudo apt install mdbtools

Answer #3:

For a one time conversion of an old .mdb file to .sqlite, I found this site helpful: . I’m not affiliated with it in any way, it’s just what I wound up with when no answers here worked for me. They offer a curl command:

curl -F files[]=@database.ext '' -o

Answered By: mattmc3

Answer #4:

This question is old but the documentation says:

The easiest way to install is using pip. Windows binaries will be downloaded but other operating systems will need to compile from source.

So it should be possible. There is also a example for linux machines.

But check out this part in the source.,L636

It shows you how the connection string for MS Access files looks like.

Answered By: gravmatt

Leave a Reply

Your email address will not be published. Required fields are marked *