skip to navigation
skip to content

datafolder 0.0.7

Install and access data files (conf, json, sqlite3, ...) in an easy way.

Latest Version: 0.3.6

datafolder is a small python library that makes it very easy to install the data files of your package and access them later.

If you want to install some data files (conf, sqlite, csv, …) to a place like the user’s home directory and find it difficult with setuptools, then here is some help.

WARNING: this is pre-alpha software!!!

First, let’s make life easier and use some ‘configuration by convention’.

I assume that (1) you have layout your project like:
    |_ mypkg.conf
    |_ mypkg.db
    |_ ...

And that (2) you want to put a folder, in the home directory of the user (in Windows will be in %APPDATA%), with your data files (conf, csv, …) inside. This folder will have the name of your package (preceded with a ‘.’ in UNIX systems), let’s say ‘.mypkg’ and, of course, with the right permissions (it will work with sudo pip install mypkg). For virtual environements the data folder will be put at the root of the environement.

How to do it?

  1. First, install the datafolder package.

  2. Then enter at a teminal:

    $ datafolder_mkboot

It will make a file called that you must put in your mypkg directory.

  1. Then, enter:

    $ datafolder_mktpl

it will create a new file called that you must put at the root of your project.

  1. That file is a template that you have to adapt to your case:
import sys
import pkg_resources
from setuptools import setup

from mypkg.bootdf import Installer                          #<-- ADAPT THIS

# write the name of the package (in this case 'mypkg'!)
MYPKG = 'mypkg'                                             #<-- ADAPT THIS

# list of data files in mypkg (just the names)
# [don't forget to include these files in!]
MYDATAFILES = ['mypkg.conf', 'mypkg.db']                    #<-- ADAPT THIS

# tell setup were these files are in your package
# (I assume that they are together with the first
MYRESOURCES = [pkg_resources.resource_filename(MYPKG, datafile)
               for datafile in MYDATAFILES]

# now, create the installer
installer = Installer(sys.argv)

# create the data folder and tell setup to put the data files there
DATAPATH = installer.data_path(MYPKG)
data_files = [(DATAPATH, MYRESOURCES)]

# now, setup can do his thing...
    packages=[MYPKG],                                       # <-- ADAPT THIS

# but we are NOT READY, in some cases the data files
# don't have the appropriate permissions,
# let us fix that...
  1. Now, rename the file to ‘’.

And that is all!

“But, I have the reverse problem, how can I access these files in my code?” I heard you say.

Very easy, in your code:

from datafolder import DataFolder

data = DataFolder('mypkg')

# now you can get the full path of each data file, e.g.
conffile = data.files['mypkg.conf']

# do your thing... (read, write, ...)

For your convinience, the DataFolder class discovers the location of the data folder for you and provides attributes and methods, that make it easy to handle the files presente in the data folder.

Feedback, please!

File Type Py Version Uploaded on Size
datafolder-0.0.7-py2.py3-none-any.whl (md5) Python Wheel 2.7 2014-07-14 7KB
datafolder-0.0.7.tar.gz (md5) Source 2014-07-14 8KB