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?
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With