How to install packages from Requirement.txt in python using anaconda?

Question:

I am confused on how to install all the packages from requirements.txt shared by another person for a python project strictly using Anaconda only in Windows os.

  1. I have installed Anaconda navigator. Should I do it in navigator or in conda prompt ?
  2. Do I need to create an environment first and then activate it and then run command pip install requirements.txt in that environment ?

Please, could you suggest a better way to install the packages from anaconda using requirements.txt and run the python project?

Asked By: kate moss

||

Answers:

In a terminal window you can enter:

pip install -r requirements.txt

You will need to enter the full path of the requirements.txt

C:Users[UserName]Desktoprequirements.txt

You can also see this described here:

https://note.nkmk.me/en/python-pip-install-requirements/

Answered By: Colin

conda uses an environment.yaml file instead of requirements.txt, but you can include one in the other:

# environment.yaml

name: test-env
dependencies:
  - python>=3.5
  - anaconda
  - pip
  - pip:
    - -r file:requirements.txt

Then use conda to create the environment via

conda env create -f environment.yaml
Answered By: Daniel Lenz

While installing packages in requirements.txt using Conda through the following command

conda install --yes --file requirements.txt
Answered By: Swarup Saha

Kate, your question and terminology should be more precise.

First, I will answer your question… Then I will go into more detail with more precise terminology for others who have the same questions.

CONVERT REQUIREMENTS.TXT TO ENVIRONMENT.YML FILE

The best way to use a conda and a requirements.txt (the pip package manager installation specification) is to convert the requirements.txt file into an environment.yml file. To do this, copy the names of all packages from the requirements.txt file into an environment.yml file. Make sure the environment.yml file is properly formatted.

See this link for examples of environment.yml file formatting:

[Creating an environment file][1]
Then use conda from the command line and specify the environment.yml file in your conda command at the console.

Try this automated script to read and use requirements.txt in conda "on the fly". [Install only available packages using "conda install –yes –file requirements.txt" without error][2]

BUILDING THE ENVIRONMENT GRAPHICALLY USING ANACONDA NAVIGATOR:

A purely graphical (and manual) alternative is to use the Anaconda Navigator Package Manager GUI. Individually select each file specified in the requirements.txt file using the Package Manager interface (see image).
Select each green check mark for the desired package in the right-hand column. Then click the "Apply" button. If the package exist in Anaconda Navigator, then this graphical approach will work.

[![Graphical Environment and Package Manager in Anaconda Navigator][3]][3]

##############################

CLARIFYING TERMINOLOGY about 5 snakes and 1 pip ;^)

There is a body of distinct terminology and semantics in this area.

  1. the "anaconda package",
  2. the "Anaconda desktop" (Navigator)
  3. the "Anaconda distribution",
  4. the "conda utility",
  5. the "conda package",
  6. the "pip utility"
  7. the "pip package",
  8. related topics (read on…)

When you say "anaconda", you probably mean the "Anaconda Navigator Desktop" graphical user interface, not the python package "anaconda" that gets installed at a terminal command line using conda or pip.

The "anaconda" (lower case) package is for supporting automated installation of the "Anaconda Distribution" and the "Anaconda Navigator GUI Desktop".

The "Anaconda Navigator" is the desktop program shipped with the Anaconda.com distribution. It provides GUI functions for managing environments and packages within conda environments.

Under the covers the Anaconda Navigator "environment manager" executes the commands using the "conda" command line utility. Navigator effectively creates and manages environments (and packages) using the conda utility. It formulates and executes commands via conda, in a similar to how you execute conda commands on the Windows Console or Mac Terminal command line interface.

The Python package named "conda" provides a programmatic interface for calling conda functions from within Python programs.

The "conda" command line utility is an environment manager; it is also a full package manager that does everything that the pip utility does.

The "pip" command line utility is a package manager for Python-only packages in pip format.

The conda utility and conda package work in a language-agnostic way. They can manage environments and packages for many other programming languages (e.g., R, JavaScript, for starters) and many others.

CONDA AT THE COMMAND LINE OR THROUGH THE NAVIGATOR?

You can use "conda" at the command line, OR you use the Navigator Environment and Package Manager GUI. I prefer the command line, because it is much faster and more precise.

BE MINDFUL WHEN USING PIP AND CONDA TOGETHER:

Be VERY careful about using pip and conda "side-by-side". They each build and manage their own package indexes. Interoperability is still an "experimental feature". The best guide to pip and conda together is here: https://www.anaconda.com/blog/using-pip-in-a-conda-environment

One thing that I usually do is install the conda and pip packages into any new conda environment first (using "conda install -c conda-forge conda pip" at the command line). After that conda then is informed about the pip package installations. I find that pip corrupts my conda environments less frequently when I do this.

See the following document for more information about using pip inside conda environments. https://docs.conda.io/projects/conda/en/stable/user-guide/configuration/pip-interoperability.html?highlight=pip

BOTTOM LINE: Take care using pip inside conda environments!

ALWAYS LEAD WITH CONDA!

USE PIP LAST, after exhausting and using all available conda packages.

Do not keep interleaving conda and pip installation commands.

If you interleave conda and pip commands repeatedly in your Anaconda root or "base" environment, you will be eventually break your Anaconda installation, and must completely remove and re-install Anaconda.

The Anaconda un-installation can be a dirty process on Windows and Mac, but luckily there are scripts to completely remove all traces of it, and then reinstall it. [Uninstall Anaconda][4]

[Conda User Manual][5]

[1]: https://carpentries-incubator.github.io/introduction-to-conda-for-data-scientists/04-sharing-environments/index.html#:~:text=Conda%20uses%20YAML%20(%E2%80%9CYAML%20Ain,style%20indentation%20to%20indicate%20nesting.
[2]: Install only available packages using "conda install –yes –file requirements.txt" without error
[3]: https://i.stack.imgur.com/5dHFL.png
[4]: https://docs.anaconda.com/anaconda/install/uninstall/
[5]: https://docs.conda.io/projects/conda/en/stable/user-guide/index.html

Answered By: Rich Lysakowski PhD
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.