48

I have package "A" with a setup.py and an extras_requires line like:

extras_require = {
    'ssh':  ['paramiko'],
},

And a package "B" that depends on util:

install_requires = ['A[ssh]']

If I run python setup.py install on package B, which uses setuptools.command.easy_install under the hood, the extras_requires is correctly resolved, and paramiko is installed.

However, if I run pip /path/to/B or pip hxxp://.../b-version.tar.gz, package A is installed, but paramiko is not.

Because pip "installs from source", I'm not quite sure why this isn't working. It should be invoking the setup.py of B, then resolving & installing dependencies of both B and A.

Is this possible with pip?

Quentin Pradet
  • 4,691
  • 2
  • 29
  • 41
dsully
  • 483
  • 1
  • 5
  • 6

3 Answers3

42

We use setup.py and pip to manage development dependencies for our packages, though you need a newer version of pip (we're using 1.4.1 currently).

#!/usr/bin/env python
from setuptools import setup
from myproject import __version__ 

required = [
    'gevent',
    'flask',
    ...
]

extras = {
    'develop': [
        'Fabric',
        'nose',
    ]
}

setup(
    name="my-project",
    version=__version__,
    description="My awsome project.",
    packages=[
        "my_project"
    ],
    include_package_data=True,
    zip_safe=False,
    scripts=[
        'runmyproject',
    ],
    install_requires=required,
    extras_require=extras,
)

To install the package:

$ pip install -e . # only installs "required"

To develop:

$ pip install -e .[develop] # installs develop dependencies
aaronfay
  • 1,673
  • 1
  • 17
  • 19
  • 8
    It appears that `pip install .[develop]` (without the `-e`) does not work, and you need to use an editable installation if you want to install extras of `.`. – Jeremy Apr 30 '14 at 15:51
  • 1
    When using `pip install -e .[develop]`, does pip install gevent and Fabric? And where is this behavior documented? – laike9m Sep 10 '15 at 17:05
  • That command will install everything, `gevent`, `flask`, `Fabric`, and `nose`. Pip internally uses `setuptools` for their build system https://pip.pypa.io/en/latest/reference/pip/?highlight=setuptools#build-system-interface. `extras_require` is an option in `setuptools` for installing 'extras' https://pythonhosted.org/setuptools/setuptools.html#declaring-extras-optional-features-with-their-own-dependencies – aaronfay Sep 14 '15 at 17:13
  • 3
    @JeremyBanks Here is the [PIP issue](https://github.com/pypa/pip/issues/1236). It has already been fixed in the code base, so it should theoretically be fixed in the newest version. – jtpereyda Jan 06 '16 at 01:41
  • Is it possible to install multiple extras_require together? Like combine optional support for 2 different things? `.[option1,option2]`? – CMCDragonkai Dec 02 '19 at 06:47
  • is it possible to ONLY install [develop]? – Dan Ciborowski - MSFT Jun 15 '21 at 00:04
22

This is suppported since pip 1.1, which was released in February 2012 (one year after this question was asked).

Quentin Pradet
  • 4,691
  • 2
  • 29
  • 41
TryPyPy
  • 6,214
  • 5
  • 35
  • 63
  • That appears to be it. My grep over the pip source tree didn't turn it up, as I was looking for the string "extras_require", not just "extras". – dsully Jan 26 '11 at 16:24
  • 9
    For posterity, use `pip install packagename[extra1,extra2]==2.1` as described in example 6 [here](https://pip.pypa.io/en/latest/reference/pip_install.html#examples) and in [this post](http://stackoverflow.com/a/26285009/1959808). – 0 _ Jan 22 '15 at 05:47
21

The answer from @aaronfay is completely correct but it may be nice to point out that if you're using zsh that the install command pip install -e .[dev] needs to be replaced by pip install -e ".[dev]".

cantdutchthis
  • 31,949
  • 17
  • 74
  • 114
  • 1
    I thought I am stupid but now I know: ["zsh uses square brackets for globbing / pattern matching."](https://stackoverflow.com/a/30539963/1611317) – Viatorus Sep 29 '20 at 20:21