I'm trying to include data files in distutils for my package and then refer to them using relative paths (following http://docs.python.org/distutils/setupscript.html#distutils-additional-files)
My dir structure is:
myproject/
mycode.py
data/
file1.dat
the code in mycode.py
, which is actually a script in the package. It relies on accessing data/file1.dat
, refer to it using that relative path. In setup.py
, I have:
setup(
...
scripts = "myproject/mycode.py"
data_files = [('data', 'myproject/data/file1.dat')]
)
suppose the user now uses:
python setup.py --prefix=/home/user/
Then mycode.py
will appear in some place like /home/user/bin/
. But the reference to data/file1.dat
is now broken, since the script lives elsewhere from the data.
How can I find out, from mycode.py
, the absolute path to myproject/data/file1.dat
, so I can refer to it properly depending on where the user installed the package?
EDIT
When I install this with prefix=/home/user/
, I get data/file1.dat
created in /home/user/
which is exactly what I want, the only missing piece is how to retrieve the absolute path to this file programmatically, given only a relative path and not knowing where the user installed the package. When I try to use package_data
instead of data_files
, it does not work - I simply don't get data/file1.dat
created anywhere, even if I delete my MANIFEST
file.
I've read all the of the current discussions of this apparently very common problem. All the proposed solutions however are not dealing with the case I have a above, where the code that needs to access data_files
is a script and its location might change depending on the --prefix
argument to setup.py
. The only hack I can think of to resolve this is to add the data file to scripts=
in setup()
, as in:
setup(
...
scripts = ["myproject/mycode.py", "myproject/data/file1.data"]
)
this is a horrible hack but it is the only way I can think of to ensure that file1.data
will be in the same place as the scripts defined in scripts=
, since I cannot find any platform independent and installation sensitive API to recover the location of data_files
after the user ran setup.py install
(potentially with --prefix=
args).