Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing extras using conda

When writing a setup.py I can specify extras_require and give a list of dependencies that are needed for additional functionality of my tool like this:

setup(
    name = "mypackage",
    install_requires = ["numpy"],
    extras_require = {
        "plotting": ["matplotlib"],
    }
)

I uploaded my package to PyPI and a conda channel and tried to install it, including all extras. From PyPI I can install the extras using:

$ pip install mypackage[plotting]

However, when installing my package from conda, I fail to install the extras. Is there a similar option for conda?

like image 247
m00am Avatar asked Apr 13 '17 10:04

m00am


People also ask

Can conda install pip packages?

You can install pip in the current conda environment with the command conda install pip , as discussed in Using pip in an environment. If there are instances of pip installed both inside and outside the current conda environment, the instance of pip installed inside the current conda environment is used.

Should I install packages with conda or pip?

Conda creates language-agnostic environments natively whereas pip relies on virtualenv to manage only Python environments Though it is recommended to always use conda packages, conda also includes pip, so you don't have to choose between the two.


2 Answers

You can do this by creating a metapackage for the optional dependency. A good example for this is the matploptlib-feedstock. Here you have the main package matplotlib-base that contains all code and the metapackage matplotlib that depends on matplotlib-base and its optional Qt dependencies.

With the matplotlib example in mind, you could have the following outputs in your recipe:

package:
  name: some_pkg

…usual recipe contents…

outputs:
  - name: some_pkg

  - name: some_pkg_with_optional_dep
    requirements:
      host:
        - python
      run:
        - python
        - optional_dependency
        - {{ pin_subpackage('some_pkg', exact=True) }}
    test:
      imports:
        - some_pkg

Note that when you require a specific version of an optional dependency, you can specify it in the optional package but then the version constraint will only be applied when you have some_pkg_with_optional_dep installed.

To have the version constraint on the optional dependency respected independently of the presence of some_pkg_with_optional_dep, you should specify it in the run_constrained section:

requirements:
  …
  run_constraint:
    - optional_dependency >=1.2
like image 52
Uwe L. Korn Avatar answered Sep 23 '22 01:09

Uwe L. Korn


As of 18-APR-2017, this is not currently possible. See https://github.com/conda/conda/issues/3299 and https://github.com/conda/conda/issues/2984

The solution that I generally use is just to make everything that might be optional as a mandatory dependency. This tends to work out, since conda packages are available for most platforms, particularly for common dependencies, and if a dependency isn't available, writing a conda recipe and uploading it to Anaconda.org is relatively easy.

like image 24
darthbith Avatar answered Sep 26 '22 01:09

darthbith