Note that there are some explanatory texts on larger screens.

plurals
  1. PODetermining the location of distutils data files programmatically in Python
    text
    copied!<p>I'm trying to include data files in distutils for my package and then refer to them using relative paths (following <a href="http://docs.python.org/distutils/setupscript.html#distutils-additional-files">http://docs.python.org/distutils/setupscript.html#distutils-additional-files</a>)</p> <p>My dir structure is:</p> <pre><code>myproject/ mycode.py data/ file1.dat </code></pre> <p>the code in <code>mycode.py</code>, which is actually a script in the package. It relies on accessing <code>data/file1.dat</code>, refer to it using that relative path. In <code>setup.py</code>, I have:</p> <pre><code>setup( ... scripts = "myproject/mycode.py" data_files = [('data', 'myproject/data/file1.dat')] ) </code></pre> <p>suppose the user now uses:</p> <pre><code>python setup.py --prefix=/home/user/ </code></pre> <p>Then <code>mycode.py</code> will appear in some place like <code>/home/user/bin/</code>. But the reference to <code>data/file1.dat</code> is now broken, since the script lives elsewhere from the data.</p> <p>How can I find out, from <code>mycode.py</code>, the absolute path to <code>myproject/data/file1.dat</code>, so I can refer to it properly depending on where the user installed the package?</p> <p><strong>EDIT</strong><br> When I install this with <code>prefix=/home/user/</code>, I get <code>data/file1.dat</code> created in <code>/home/user/</code> 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 <code>package_data</code> instead of <code>data_files</code>, it does not work - I simply don't get <code>data/file1.dat</code> created anywhere, even if I delete my <code>MANIFEST</code> file. </p> <p>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, <strong>where the code that needs to access <code>data_files</code> is a script</strong> and its location might change depending on the <code>--prefix</code> argument to <code>setup.py</code>. The only hack I can think of to resolve this is to add the data file to <code>scripts=</code> in <code>setup()</code>, as in:</p> <pre><code>setup( ... scripts = ["myproject/mycode.py", "myproject/data/file1.data"] ) </code></pre> <p>this is a horrible hack but it is the only way I can think of to ensure that <code>file1.data</code> will be in the same place as the scripts defined in <code>scripts=</code>, since I cannot find any platform independent and installation sensitive API to recover the location of <code>data_files</code> after the user ran <code>setup.py install</code> (potentially with <code>--prefix=</code> args).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload