Absolute path error when building wheel

ghz 昨天 ⋅ 2 views

OVERVIEW

I'm trying to learn how to build wheels on my windows dev box so hopefully I'll have a nice way to deploy django websites on linux boxes. But right now I'm stuck with a little error.

Here's my setup.py:

from setuptools import setup, find_packages

setup(name='pkg',
      version="1.0",
      packages=find_packages(),
      data_files=[('/etc/nginx/sites-available', ['foo.conf'])]
      )

When i try to do >python setup.py bdist_wheel I'm getting this error:

raise ValueError, "path '%s' cannot be absolute" % pathname

It seems the way I'm using data_files is not supported.

QUESTION

What's the right way to deploy config files using wheels & setup.py?

Answer

The error you're encountering is due to the use of absolute paths in the data_files parameter in setup.py. When building a wheel distribution, you cannot use absolute paths, as they are platform-dependent. Instead, you need to use relative paths for the data_files parameter.

In the case of packaging config files (e.g., for Nginx), it's common to use the data_files parameter to specify the destination directory on the target system, but the source files should be part of the package itself.

Here’s how you can fix it:

  1. Place the config file inside your package directory. For example, if you want to include foo.conf, put it in a subdirectory like config/.

  2. Use relative paths in the data_files section.

Here’s an updated version of your setup.py:

from setuptools import setup, find_packages

setup(
    name='pkg',
    version="1.0",
    packages=find_packages(),
    # Use a relative path and specify where the file should be installed
    data_files=[('etc/nginx/sites-available', ['config/foo.conf'])]
)

In this example:

  • The foo.conf file is located in the config/ directory (relative to your setup.py).
  • During installation, the file will be installed to etc/nginx/sites-available on the target system.

To build the wheel, run:

python setup.py bdist_wheel

Installing the wheel

To install the wheel on a Linux machine, you can use pip:

pip install your_package.whl

This will install the config file (foo.conf) to the correct location (e.g., /etc/nginx/sites-available).

Additional Notes:

  • If you're packaging files that need to go to specific directories (like /etc), ensure that your users have the appropriate permissions to write to those directories when installing the package.
  • For deploying on Linux, you may also want to handle symlinks (for Nginx config, for example) using post-installation scripts or documentation.