Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python setuptools: how to include a config file for distribution into <prefix>/etc

How can I write setup.py so that:

  1. The binary egg distribution (bdist_egg) includes a sample configuration file and
  2. Upon installation puts it into the {prefix}/etc directory?

A sample project source directory looks like this:

bin/    myapp etc/    myapp.cfg myapp/     __init__.py     [...] setup.py 

The setup.py looks like this:

from distutils.command.install_data import install_data  packages = ['myapp', ] scripts = ['bin/myapp',] cmdclasses = {'install_data': install_data} data_files = [('etc', ['etc/myapp.cfg'])]  setup_args = {     'name': 'MyApp',     'version': '0.1',     'packages': packages,     'cmdclass': cmdclasses,     'data_files': data_files,     'scripts': scripts, #    'include_package_data': True,     'test_suite': 'nose.collector' }  try:     from setuptools import setup except ImportError:     from distutils.core import setup  setup(**setup_args) 

setuptools are installed in both the build environment and in the installation environment.

The 'include_package_data' commented out or not does not help.

like image 883
Victor Olex Avatar asked May 04 '12 21:05

Victor Olex


People also ask

How do I add config files to Python?

Python can have config files with all settings needed by the application dynamically or periodically. Python config files have the extension as . ini. We'll use VS Code (Visual Studio Code) to create a main method that uses config file to read the configurations and then print on the console.

How do I include a file in a Python package?

Place the files that you want to include in the package directory (in our case, the data has to reside in the roman/ directory). Add the field include_package_data=True in setup.py. Add the field package_data={'': [... patterns for files you want to include, relative to package dir...]} in setup.py .

What is setup CFG Python?

setup. cfg is a cheekily named Python package which supports providing all of a Python distribution's metadata and build configuration via the setup. cfg file at the base of the distribution's source tree, rather than in the setup.py script. The standard setup.py script is reduced to a stub which uses the setup.

Where do I put setup py?

To install a package that includes a setup.py file, open a command or terminal window and: cd into the root directory where setup.py is located. Enter: python setup.py install.


1 Answers

I was doing some research on this issue and I think the answer is in the setuptools documentation: http://peak.telecommunity.com/DevCenter/setuptools#non-package-data-files

Next, I quote the extract that I think has the answer:

Non-Package Data Files

The distutils normally install general "data files" to a platform-specific location (e.g. /usr/share). This feature intended to be used for things like documentation, example configuration files, and the like. setuptools does not install these data files in a separate location, however. They are bundled inside the egg file or directory, alongside the Python modules and packages. The data files can also be accessed using the Resource Management API [...]

Note, by the way, that this encapsulation of data files means that you can't actually install data files to some arbitrary location on a user's machine; this is a feature, not a bug. You can always include a script in your distribution that extracts and copies your the documentation or data files to a user-specified location, at their discretion. If you put related data files in a single directory, you can use resource_filename() with the directory name to get a filesystem directory that then can be copied with the shutil module. [...]

like image 171
Akhorus Avatar answered Sep 21 '22 15:09

Akhorus