17

I want to create a setup.py which would install my files into custom directories. I have a certain prefix, where I would like to get the following result:

/my/prefix/
  bin/
    script.sh
  libexec/
    one.py
    two.py
    ...
  lib/pythonX.Y/site-packages/
    package/...

My initial project is following:

/
  script.sh
  one.py
  two.py
  ...
  setup.py
  package/...
    __init__.py
    ...

What would be the best way to achieve that? I would like to be able to install it later with something like:

python setup.py install --prefix=/my/prefix

I can get "package" nicely installed in the correct directory as lib/pythonX.Y/site-packages under --prefix is the default location. But is there a clean way to get script.sh into "bin" and other python files into "libexec"? The only way I see to achieve that would be to manually copy those files in my setup.py script. Maybe there is a cleaner and more standard way to do that?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Ago
  • 1,101
  • 3
  • 12
  • 22
  • There's some useful docs on this here: https://docs.python.org/2/install/index.html#alternate-installation-unix-the-prefix-scheme and here https://docs.python.org/3/distutils/introduction.html – Pierz Feb 28 '18 at 11:13
  • With `setuptools` see https://stackoverflow.com/q/24745852/520567 – akostadinov May 05 '22 at 21:54

2 Answers2

10

The scripts are handled by use of the scripts parameter to the setup function. For libexec you can treat them as data files and use a data options.

setup(...
    scripts=glob("bin/*"),
    data_files=[(os.path.join(sys.prefix, 'libexec', 'mypackage'), glob("libexec/*"))],
    ...
)

I'm not sure how that would work with a --prefix option, I've never tried that.

Keith
  • 42,110
  • 11
  • 57
  • 76
  • Thanks! data_files did it for me. Another question, can I have a default "prefix"? If the user doesn't specify one, I would like to use my own (instead of the python default). – Ago May 05 '12 at 08:30
  • 1
    You can make option defaults in the setup.cfg file. It's in .ini style option file with sections and name-value pairs. Command line options directly translate to options there (remove `--`, etc). – Keith May 05 '12 at 16:13
  • What's the best way to refer to `libexec` inside some of the files in the Python package if you are going to use `libexec` here in the `setup.py`? Can `setup.py` do macro substitution in the same vein as autoconf `@libexecdir@`? – CMCDragonkai Dec 13 '19 at 03:31
1

I ended up with setup.py like that:

setup(name='mylib',
  scripts=['script.sh'],
  data_files=[('libexec', ['one.py', 'two.py'])]
)

Of course, you could iterate over all python files for libexec, but I only have 2-3 python files I need there.


Additionally, I can have setup.cfg with the following:

[install]
prefix=/my/prefix

and instead of python setup.py install --prefix=/my/prefix I can just do:

python setup.py install

This answer was posted as edits (edit 1, edit 2) to the question python setup.py configuration to install files in custom directories by the OP Ago under CC BY-SA 3.0.

vvvvv
  • 25,404
  • 19
  • 49
  • 81