[Distutils] Cross-platform way to get default directory for binary files like console scripts?

Paul Moore p.f.moore at gmail.com
Fri Feb 21 19:45:19 CET 2014


On 21 February 2014 17:04, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
> On 21 February 2014 13:24, Paul Moore <p.f.moore at gmail.com> wrote:
>>>
>>> Is there cross-platform way to get default directory for binary files
>>> (console scripts for instance) the same way one can use sys.executable
>>> to get path to the Python's interpreter in cross-platform way?
>>
>> sysconfig.get_path("scripts") should work. If you're on a Python too
>> old to have sysconfig then sorry, I've no idea (other than "you should
>> upgrade" :-))
>
> Ah, well that's better.
>
> One question though: are these guaranteed to be consistent. I was
> pointing at the actual code that distutils uses when installing where
> as you're pointing at a module that independently lists an
> over-lapping set of data:
> http://hg.python.org/cpython/file/005d0678f93c/Lib/sysconfig.py#l21
> http://hg.python.org/cpython/file/005d0678f93c/Lib/distutils/command/install.py#l28
>
> For example sysconfig defines a scheme 'osx_framework_user' that
> doesn't appear in distutils.command.install and uses slightly
> different paths from posix_user. Does that mean that it is
> inconsistent with what distutils would do?

Well, it's difficult to tell. As you say, there are "schemes" involved
- the original question isn't actually well-defined as there are
"user" and "system" installs on each platform:

>>> sysconfig.get_path("scripts")
'C:\\Apps\\Python33\\Scripts'
>>> sysconfig.get_path("scripts", "nt_user")
'C:\\Users\\Gustav\\AppData\\Roaming\\Python\\Scripts'
>>> sysconfig.get_path("scripts", "nt")
'C:\\Apps\\Python33\\Scripts'

So the best you can really say is that sysconfig lets you get the
script path for a given scheme. But distutils predates sysconfig (and
setuptools hacks the internals of distutils in ways that potentially
add even more variability) so no, I can't guarantee that distutils
might not be consistent. If the two did give different results it's
probably a bug, though. But whether it's a bug taht would get *fixed*
is yet another problem (compatibility and all that).

For the use case described by the OP, using sysconfig is probably
sensible. But the tool should likely document that it won't find
executables installed with --user, and maybe provide a flag to say to
use the user scheme rather than the default scheme, so that the user
can control things. Depending on the complexity of the tool, and the
importance of being able to cope with unusual situations, having a
config file that allows the user to specify the full path on a
program-by-program basis might be necessary.

In summary - sysconfig is what you should use if you want the
"official" answer. But the question is not as easy as it looks.

Paul


More information about the Distutils-SIG mailing list