Skip to main content

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

Project description

Built Status

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 beta software!

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

I assume that (1) you have layout your project like:

MANIFEST.in
README.rst
setup.py
mypkg
    │
    ├── __init__.py
    ├── 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:

    $ pip install -U datafolder
  2. Then, type:

    $ datafolder_mkboot

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

  3. Then, type:

    $ datafolder_mktpl

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

  4. 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 MANIFEST.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 __init__.py)
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...
setup(
    name=MYPKG,
    packages=[MYPKG,'other_packg1','other_packg2'],        # <-- ADAPT THIS
    data_files=data_files,
    ...                                                    # <-- ADAPT THIS
)

# but we are NOT READY, in some cases the data files
# don't have the appropriate permissions,
# let us fix that...
installer.pos_setup(MYDATAFILES)
  1. Now, rename the file to setup.py.

  2. Write your README.in file (missing this step is the cause of many problems!). Should look like this:

include *.txt
include *.md
include *.rst
recursive-include mypkg *.conf
recursive-include mypkg *.rst
recursive-include mypkg *.db

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 bootdf 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.

To install, type:

$ pip install -U datafolder

Feedback, please!

REMARK: as you can see above, this only works if the install method uses setup.py. Is not the case of python wheels however!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

datafolder-0.1.3.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

datafolder-0.1.3-py2.py3-none-any.whl (6.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file datafolder-0.1.3.tar.gz.

File metadata

  • Download URL: datafolder-0.1.3.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for datafolder-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e641276c97a48284221b539cf33b9940eecd5832a931b28ab1a33f71f18ba6ff
MD5 713cf41a9d0dc32793f6cb2173a0612b
BLAKE2b-256 a433c36782183182557d5f540c2e68d377ccbce8ba5861a4e46770253a001982

See more details on using hashes here.

File details

Details for the file datafolder-0.1.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for datafolder-0.1.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b8372f1d6a3ad1871161f469cc1c707cb9066cc531646563f08b367faccc2af9
MD5 b9f7bf156fc64a3e93ec804da49db387
BLAKE2b-256 c64d82e17132b622135496b5db263be4abe359bcb40810f59ec26eb74e7950d5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page