Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the confusion arises from the usage of scripts. Scripts should refer to a runnable executable, perhaps a utility script related to your package or perhaps an entry point into functionality for your package. In either case, you should expect that any scripts will not be installed alongside the rest of your package. This expectation is due mainly to the convention that packages are considered libraries (and installed to lib directories) whereas scripts are considered executables (and installed to bin or Scripts directories). Furthermore, data files are neither executables nor libraries and are completely separate.</p> <p>So from the script, you need to determine where the data files are located. According to the <a href="http://docs.python.org/distutils/setupscript.html#installing-additional-files" rel="nofollow">Python docs</a>,</p> <blockquote> <p>If directory is a relative path, it is interpreted relative to the installation prefix.</p> </blockquote> <p>Therefore, you should write something like the following in the mycode script to locate the data file:</p> <pre><code>import sys import os def my_func(): with open(os.path.join(sys.prefix, 'data', 'file1.dat')) as f: print(next(f)) if __name__ == '__main__': my_func() </code></pre> <p>If you're not pleased with the way that your code and data are not bundled together (and I would not be), then I would restructure your package so that you have an actual Python package (and module) and use packages= and package_data= to inject the data into the package, and then create a simple script that calls into the module in the package.</p> <p>I did that by creating this tree:</p> <pre><code>. │ setup.py │ ├───myproject │ │ mycode.py │ │ __init__.py │ │ │ └───data │ file1.dat │ └───scripts run-my-code.py </code></pre> <p>With setup.py:</p> <pre><code>from distutils.core import setup setup( name='myproject', version='1.0', scripts=['scripts/run-my-code.py'], packages=['myproject'], package_data = { 'myproject': ['data/file1.dat'], }, ) </code></pre> <p>run-my-code.py is simply:</p> <pre><code>from myproject import mycode mycode.my_func() </code></pre> <p><code>__init__</code> is empty and mycode.py looks like:</p> <pre><code>import os here = os.path.dirname(__file__) def my_func(): with open(os.path.join(here, 'data', 'file1.dat')) as f: print(next(f)) </code></pre> <p>This latter approach keeps the data and code bundled together (in site-packages/myproject) and only installs the script in a different location (so it shows up in the $PATH).</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