Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a pip requirements file in a conda yml file throws AttributeError: 'FileNotFoundError'

Tags:

python

pip

conda

I have a requirements.txt like

numpy

and an environment.yml containing

# run via: conda env create --file environment.yml
---
name: test
dependencies:
  - python>=3
  - pip
  - pip:
      - -r file:requirements.txt

when I then run conda env create --file environment.yml I get

Pip subprocess output:

Pip subprocess error: ERROR: Exception:

<... error traceback in pip >

AttributeError: 'FileNotFoundError' object has no attribute 'read'

failed

CondaEnvException: Pip failed

It is also strange how pip is called, as reported just before the error occurs:

['$HOME/.conda/envs/test/bin/python', '-m', 'pip', 'install', '-U', '-r', '$HOME/test/condaenv.8d3003nm.requirements.txt']

(I replace my home path with $HOME) Note the weird expansion of the requirements.txt.

Any ideas?

like image 246
Stefan Avatar asked Jul 29 '21 07:07

Stefan


People also ask

Why Pip is not installed by default in conda?

When we create a new conda environment using pip is not installed by default. However, pip is usually installed on a user level, which means you could run the command in this environment. But this will install all requirements to the python path globally instead of in the environment.

Can Pip run in a YAML file?

For example, Basically, any option you can run with pip install you can run in a YAML. See the Advanced Pip Example for a showcase of other capabilities. Pip v21.2.1 introduced stricter behavior for URI parsing and no longer supports this. See this answer for details.

How to install requirements txt in conda?

To install requirements.txt in the environment, we have to use the pip installed within the environment. Thus we should install pip first by. conda install pip. Then we can install the requirements.txt. Of course, there is a better way. We simply create a conda environment with pip installed in it: conda create -n yourenv pip.

How to install requirements in Python using pip?

pip is not installed by default. However, pip is usually installed on a user level, which means you could run the command in this environment. But this will install all requirements to the python path globally instead of in the environment. To install requirements.txt in the environment, we have to use the pip installed within the environment.


1 Answers

Changes to Pip Behavior in 21.2.1

A recent change in the Pip code has changed its behavior to be more strict with respect to file: URI syntax. As pointed out by a PyPA member and Pip developer, the syntax file:requirements.txt is not a valid URI according to the RFC8089 specification.

Instead, one must either drop the file: scheme altogether:

name: test
dependencies:
  - python>=3
  - pip
  - pip:
    - -r requirements.txt

or provide a valid URI, which means using an absolute path (or a local file server):

name: test
dependencies:
  - python>=3
  - pip
  - pip:
    - -r file:/full/path/to/requirements.txt
    # - -r file:///full/path/to/requirements.txt # alternate syntax
like image 175
merv Avatar answered Oct 15 '22 22:10

merv