问题
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 path to the script (e.g. on windows it'll be something like C:/my/virtual/env/scripts/my_console_script.exe). I need this so I can pass that script path as an argument to other commands, regardless of where the package is installed. Setuputils provides the pkg_resources, but that doesn't seem to expose any way of actually getting at the raw installed paths, only loadable objects.
Edit: To make the use case plain here's the setup.
I have a plugin-driven application that communicates with various local services. One of these plug-ins ties into the alerting interface of an NMS package. The only way this alerting package can get alerts out to an arbitrary handler is to call a script - the path to execute (the console_scripts entry point in this case) is register as a complete path - that's the path I need to get.
回答1:
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
.pyin the package makes it easy to get at from Python code anywhere.On the surface it looks like a bit of a hack. However,
setuptoolsis specifically designed to allow custom command classes. Nothing here is really accessing anything private, except maybe the value passing using thedistributionobject which is just to avoid aglobal.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.
sdistis safe though.developis possible but needs another command class.installisn't called andbuild_pyis only called if you setuse_2to3. A customdevelopcommand can get the path as in theinstallexample, but asself.script_dir. From there you have to decide if you're comfortable writing a file to your source directory, which is inself.egg_path-- as it will get picked up byinstalllater, 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.
回答2:
You could get the path for site_packages directory as follows:
from distutils.sysconfig import get_python_lib
get_python_lib()
If your script is somewhere relative to that path, either in site-packages or up a directory you could then join the relative path with the site-packages path as follows:
import os
scriptPath = os.path.realpath(os.path.join(get_python_lib(), '../my/relative/path/script.exe'))
Another trick you could try is to import the associated module and then look at its file parameter. You could then look for your script the same way as the example above if you know what the relative path is to that file. Try it on the command line interface..
import django
django.__file__
/usr/lib/python2.6/site-packages/django/init.pyc
modulePath = os.path.split(django.__file__)[0]
scriptPath = os.path.realpath(os.path.join(modulePath, '../my/relative/path/script.exe'))
Is the above advice on the right track?
回答3:
Have you tried `os.path.abspath(__file__)' in your entry point script? It'll return yours entry point absolute path.
Or call find_executable from distutils.spawn:
import distutils.spawn
distutils.spawn.find_executable('executable')
来源:https://stackoverflow.com/questions/25066084/get-entry-point-script-file-location-in-setuputils-package