Skip to content

Commit 8b3a493

Browse files
committed
dependencies: Updated tips and tricks
1 parent 59a4b64 commit 8b3a493

File tree

1 file changed

+99
-3
lines changed

1 file changed

+99
-3
lines changed

content/dependencies.rst

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,24 @@ shows which packages are the important packages needed by the software.
529529
530530
$ conda env export > environment.yml
531531
532+
If package build versions are not relevant for the use case,
533+
one can also run
534+
535+
.. code-block:: console
536+
537+
$ conda env export --no-builds > environment.yml
538+
539+
which leaves out the package build versions.
540+
541+
Alternatively one can also run
542+
543+
.. code-block:: console
544+
545+
$ conda env export --from-history > environment.yml
546+
547+
which creates the ``environment.yml``-file based on
548+
what packages were asked to be installed.
549+
532550
.. admonition:: conda-lock
533551

534552
For even more reproducibility, you should try out
@@ -564,9 +582,87 @@ Exercise 4
564582
Additional tips and tricks
565583
--------------------------
566584

567-
- Conda can also read and write ``requirements.txt``.
568-
- ``requirements.txt`` can also refer to packages on Github.
569-
- ``environment.yml`` can also contain a ``pip`` section.
585+
.. tabs::
586+
587+
.. group-tab:: Creating a conda environment from requirements.txt
588+
589+
conda supports installing an environment from ``requirements.txt``.
590+
591+
.. code-block:: console
592+
593+
$ conda env create --name my-environment --channel conda-forge --file requirements.txt
594+
595+
To create an ``environment.yml`` from this environment that mimics
596+
the ``requirements.txt``, activate it and run
597+
598+
.. code-block:: console
599+
600+
$ conda env export --from-history > environment.yml
601+
602+
.. group-tab:: Adding pip packages into conda environments
603+
604+
conda supports installing pip packages in an ``environment.yml``.
605+
606+
Usually this is done to add those packages that are missing
607+
from conda channels.
608+
609+
To do this you'll want to install ``pip`` into the environment
610+
and then add pip-installed packages to a list called ``pip``.
611+
612+
See this example ``environment.yml``:
613+
614+
.. code-block:: yaml
615+
616+
name: my-environment
617+
channels:
618+
- conda-forge
619+
dependencies:
620+
- python
621+
- pip
622+
- pip:
623+
- numpy
624+
- matplotlib
625+
- pandas
626+
627+
One can even add a full ``requirements.txt`` to the environment:
628+
629+
.. code-block:: yaml
630+
631+
name: my-environment
632+
channels:
633+
- conda-forge
634+
dependencies:
635+
- python
636+
- pip
637+
- pip:
638+
- "-r requirements.txt"
639+
640+
Do note that in both methods the pip-packages come from PyPI
641+
and not from conda channels. The installation of these packages
642+
is done after conda environment is created and this can also
643+
remove or update conda packages installed previously.
644+
645+
.. group-tab:: Installing pip packages from GitHub
646+
647+
Packages available in GitHub or other repositorios
648+
can be given as a URL in ``requirements.txt``.
649+
650+
For example, to install a development version of the
651+
`black code formatter <https://github.com/psf/black>`__, one can
652+
write the following ``requirement.txt``.
653+
654+
.. code-block:: txt
655+
656+
git+https://github.com/psf/black
657+
658+
or
659+
660+
.. code-block:: txt
661+
662+
https://github.com/psf/black/archive/master.zip
663+
664+
First one would use git to clone the repository, second would
665+
download the zip archive of the repository.
570666

571667

572668
How to communicate the dependencies as part of a report/thesis/publication

0 commit comments

Comments
 (0)