[Python-ideas] High time for a builtin function to manage packages (simply)?

Nick Coghlan ncoghlan at gmail.com
Mon Sep 7 07:09:03 CEST 2015


On 7 September 2015 at 12:18, Steven D'Aprano <steve at pearwood.info> wrote:
> On Sat, Sep 05, 2015 at 05:03:36PM -0400, Terry Reedy wrote:
>> On 9/5/2015 3:08 AM, Stephen J. Turnbull wrote:
>>
>> >So let's fix it, already![1]  Now that we have a blessed package
>> >management module, why not have a builtin that handles the simple
>> >cases?  Say
>> >
>> >     def installer(package, command='install'):
>> >         ...
>>
>> Because new builtins have a high threashold to reach, and this doesn't
>> reach it? Installation is a specialized and rare operation.
>
> You're right about the first part, but as Chris has already suggested,
> this need not be *literally* a built-in. Like help() it could be
> imported at REPL startup.

Technically it's "import site" that injects those - you have to run
with "-S" to prevent them from being installed:

$ python3 -c "quit()"
$ python3 -Sc "quit()"
Traceback (most recent call last):
 File "<string>", line 1, in <module>
NameError: name 'quit' is not defined

Regardless, I agree a "site builtin" like help() or quit() is a better
option here than a true builtin, and I also think it's a useful idea.

I'd make it simpler than the proposed API though, and instead just
offer an "install(specifier)" API that was a thin shell around
pip.main:

    try:
        import pip
    except ImportError:
        pass
    else:
        def install(specifier):
            cmd = ["install"]
            if sys.prefix == sys.base_prefix:
                cmd.append("--user") # User installs only when outside a venv
            cmd.append(specifier)
            # TODO: throw exception when there's a problem
            pip.main(cmd)

If folks want more flexibility, then they'll need to access (and
understand) the underlying installer.

As far as other possible objections go:

* the pkg_resources global state problem we should be able to work
around just by reloading pkg_resources (if already loaded) after
installing new packages (I've previously tried to address some aspects
of that particular problem upstream, but doing so poses significant
backwards compatibility challenges)

* I believe integration with systems like conda, PyPM, and the
Enthought installer should be addressed through a plugin model in pip,
rather than directly in the standard library

* providing a standard library API for querying the set of installed
packages independently of pip is a separate question

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list