So I have an entry point defined in my setup.py [console_scripts] section. The command is properly installed and works fine, but I need a way to programatically find out the pat
Well, you could pass an option of the form --install-option="--install-scripts=/usr/local/bin"
to pip
and just set the path yourself. But I understand why you might not want to hardcode that in a cross-platform project.
So, we just need to find out what directory setuptools
is actually using. Unfortunately, the path is determined in the middle of a whole bunch of actual setup code, rather than in a separate function we can just call.
So the next step is to write a custom install command that observes and saves the path. (For that matter, you could also set self.install_scripts
to a directory of your choice in this custom installer class, thereby keeping that piece of config in one place (the package) rather than in the package and in a command line arg to setup...)
Example:
from setuptools import setup
from setuptools.command.install import install
from distutils.command.build_py import build_py
import os
class InstallReadsScriptDir(install):
def run(self):
self.distribution._x_script_dir = self.install_scripts
install.run(self)
class BuildConfiguresScriptDir(build_py):
def run(self):
build_py.run(self)
if self.dry_run:
return
script_dir = self.distribution._x_script_dir # todo: check exists
target = os.path.join(self.build_lib, 'mypackage', 'scriptdir.py')
with open(target, 'w') as outfile:
outfile.write('path = """{}"""'.format(script_dir))
setup(
name="MyPackage",
version='0.0.1',
packages = ['mypackage'],
entry_points = {
'console_scripts': [
'footest = mypackage:main',
],
},
cmdclass= {
'install': InstallReadsScriptDir,
'build_py': BuildConfiguresScriptDir,
},
)
Possible objections:
Some people don't like any form of code generation. In this case it's more like configuration, though putting it in a .py
in the package makes it easy to get at from Python code anywhere.
On the surface it looks like a bit of a hack. However, setuptools
is specifically designed to allow custom command classes. Nothing here is really accessing anything private, except maybe the value passing using the distribution
object which is just to avoid a global
.
Only gets the directory, not the executable names. You should know the names though, based on your entry point configuration. Getting the names would require calling the parsing methods for your entry point spec and generating more code.
It's going to need some bugfixing if you e.g. build without installing. sdist
is safe though.
develop
is possible but needs another command class. install
isn't called and build_py
is only called if you set use_2to3
. A custom develop
command can get the path as in the install
example, but as self.script_dir
. From there you have to decide if you're comfortable writing a file to your source directory, which is in self.egg_path
-- as it will get picked up by install
later, though it should be the same file (and the build command will overwrite the file anyway)
Addendum
This little flash of insight may be more elegant as it does not require saving the path anywhere, but still gets the actual path setuptools uses, though of it assumes no configuration has changed since the install.
from setuptools import Distribution
from setuptools.command.install import install
class OnlyGetScriptPath(install):
def run(self):
# does not call install.run() by design
self.distribution.install_scripts = self.install_scripts
def get_setuptools_script_dir():
dist = Distribution({'cmdclass': {'install': OnlyGetScriptPath}})
dist.dry_run = True # not sure if necessary, but to be safe
dist.parse_config_files()
command = dist.get_command_obj('install')
command.ensure_finalized()
command.run()
return dist.install_scripts
Addendum 2
Just realized/remembered that pip
creates an installed-files.txt
in the egg-info
, which is a list of paths relative to the package root of all the files including the scripts. pkg_resources.get_distribution('mypackage').get_metadata('installed-files.txt')
will get this. It's only a pip
thing though, not created by setup.py install
. You'd need to go through the lines looking for your script name and then get the absolute path based on your package's directory.