Package: datafolder
datafolderis a small python library that makes it very easy toinstallthedata filesof your package andaccessthem 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 withsetuptools, then here
is some help.WARNINGthis 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 withsudo pip install mypkg). For virtual environements the
data folder will be put at the root of the environement.How to do it?First, install thedatafolderpackage:$ pip install -U datafolderThen, type:$ datafolderIt will make a file calledbootdf.pythat youmustput inside yourmypkgdirectory and
a new file calledsetup_TPL.pythat youmustput in the root of your project.setup_TPL.pyis a template that you have to adapt to your case:importsysimportpkg_resourcesfromsetuptoolsimportsetupfrommypkg.bootdfimportInstaller# <-- 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)fordatafileinMYDATAFILES]# now, create the installerinstaller=Installer(sys.argv)# create the data folder and tell setup to put the data files thereDATAPATH=installer.data_path(MYPKG)data_files=[(DATAPATH,MYRESOURCES)]# now, setup can do his thing...setup(name=MYPKG,packages=[MYPKG,'other_packg1','other_packg2'],# <-- ADAPT THISdata_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)Now,renamethe file tosetup.py.Write yourMANIFEST.infile (missing this step is the cause of many problems!). Should look like this:include *.txt
include *.md
include *.rst
include mypkg/*.conf
include mypkg/*.rst
include mypkg/*.dbAnd that is all!Butwith version 0.2.1it iseven better!Justgo to the root of you projectand in a terminal type:$ datafolder mypkgNow, you will see thatMANIFEST.inandsetup.pywere fill in for you andbootdf.pyis already inside themypkgfolder.You only need to completesetup.pyas need(the fieldsauthor,email,urland trove classifiers …).“But,I have the reverse problem, how can I access these files in my code?”
I heard you say.Very easy, in your code (for a file in the same folder asbootdf.py):from.bootdfimportDataFolderdata=DataFolder('mypkg')# now you can get the full path of each data file, e.g.conf_fp=data.files['mypkg.conf']# do your thing... (read, write, ...)For your convinience, theDataFolderclassdiscoversthe 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!REMARK: as you can see above, this only works if the
install method usessetup.py. Isnotthe case
ofpython wheelshowever!