2

In setup.py I've specified package_data as the following:

packages=['londontweetgrabber'],
package_dir={'londontweetgrabber': 'londontweetgrabber'},
package_data={'londontweetgrabber': ['data/data.db']},

My directory hierarchy is roughly as follows:

londontweetgrabber/
 |
 | docs/
 | ...
 | londontweetgrabber/
    | 
    | __init__.py
    | authentication.py
    | data
        |
        | data.db
 |
 | README
 | setup.py

What I'd like to do is use sqlite3 to load the file in data.db. Trying a variation of a solution found here (How do I use data in package_data from source code?) I can verify that the data is being loaded now, but sqlite is complaining about a table not existing that I know does. I want this data.db file to be distributed with the python package I am writing as I depend on it for what I want to do.

The code I have at the moment for loading and failing to load sqlite:

import sqlite3
import pkgutil


def get_conn():
    data = pkgutil.get_data('londontweetgrabber', 'data/data.db')
    print data

    return sqlite3.connect(data)

Thanks

0

1 Answer 1

3

You're actually passing data (the contents of data.db) to sqlite3.connect(). It however expects a path to your DB like /data/data.db.

See pkgutil.get_data():

The function returns a binary string that is the contents of the specified resource.

You can instead use pkg_resources.resource_filename() to build a proper filesystem path to your data.db:

import sqlite3
import pkg_resources

db_path = pkg_resouces.resource_filename('londontweetgrabber', 'data/data.db')
conn = sqlite3.connect(db_path)
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.