Referring to Variables Inside setup.cfg
You can refer to other variables/options inside your setup.cfg
file. The syntax is $variable
, for instance:
[install]
prefix = /my/prefix
install-scripts = $base/scripts
Please note that I used $base
since this variable is affected by your prefix settings provided both in the setup.cfg
and using the setup.py install --prefix
command line option. In this case, your scripts will be installed to /my/prefix/scripts
unless the user specifies another prefix using command line.
It is possible to use $prefix
inside setup.cfg
as well, but this does not seem to be affected by the custom configuration.
Accessing Values of Variables Within setup.py (after setup()
)
It is also possible to read the values of all variables/options inside your setup.py
. The function setup
returns an instance of the class Distribution. That class holds all the values of variables grouped by the command (e.g. install) and you can obtain them using the get_option_dict
method. For instance:
#!/usr/bin/env python
from distutils.core import setup
d = setup(
# Here come your setup arguments
)
print(d.get_option_dict('install'))
will print:
{'prefix': ('setup.cfg', '/my/prefix'),
'install_scripts': ('setup.cfg', '$base/scripts')}
Accessing Values of Variables Within setup.py (before setup()
)
It is also possible to obtain an instance of the class Distribution before setup()
is even run. To do so, we can replicate what setup()
is doing internally and build an instance of that class ourselves. Then, a decision about the value of setup arguments can be based on the value of some installation options. Let's look at an example:
from distutils.core import setup
from distutils.dist import Distribution
# Get our own instance of Distribution
dist = Distribution()
dist.parse_config_files()
dist.parse_command_line()
# Get prefix from either config file or command line
prefix = dist.get_option_dict('install')['prefix'][1]
print("Prefix is: " + prefix)
# Use the prefix to decide on the data path for data.txt
setup(data_files=[('/first/path/to/data' if prefix == '/some/prefix'
else '/second/path/to/data',
['data.txt'])])