question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'file:' identifier in '--requirements' argument yields FileNotFoundError

See original GitHub issue

Description

Using a file: identifier when providing a --requirements|-r argument results in FileNotFoundError.

Expected behavior

The file should be found.

Previous versions worked fine. Alternatively, omitting the file: identifier works.

pip version

21.2.1

Python version

3.9.6

OS

MacOS 10.14

How to Reproduce

  1. Create and activate Conda environment with:

    conda create -n foo -c conda-forge python=3.9 pip=21.2.1
    conda activate foo
    
  2. Create requirements.txt:

    echo "numpy" > requirements.txt
    
  3. Run install command:

    pip install -r file:requirements.txt
    
  4. Error.

Output

(foo) bash-3.2$ pip install -r file:requirements.txt

ERROR: Exception:                                                                                                                                                                         
Traceback (most recent call last):                                                                                                                                                        
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/cli/base_command.py", line 173, in _main                                                            
    status = self.run(options, args)                                                                                                                                                      
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/cli/req_command.py", line 203, in wrapper                                                           
    return func(self, options, args)                                                                                                                                                      
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/commands/install.py", line 286, in run                                                              
    reqs = self.get_requirements(args, options, finder, session)                                                                                                                          
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/cli/req_command.py", line 384, in get_requirements                                                  
    for parsed_req in parse_requirements(                                                                                                                                                 
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/req/req_file.py", line 135, in parse_requirements                                                   
    for parsed_line in parser.parse(filename, constraint):                                                                                                                                
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/req/req_file.py", line 319, in parse                                                                
    yield from self._parse_and_recurse(filename, constraint)                                                                                                                              
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/req/req_file.py", line 324, in _parse_and_recurse                                                   
    for line in self._parse_file(filename, constraint):                                                                                                                                   
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/req/req_file.py", line 353, in _parse_file                                                          
    _, content = get_file_content(filename, self._session)                                                                                                                                
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/req/req_file.py", line 518, in get_file_content                                                     
    resp = session.get(url)                                                                                                                                                               
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_vendor/requests/sessions.py", line 555, in get                                                               
    return self.request('GET', url, **kwargs)                                                                                                                                             
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_internal/network/session.py", line 454, in request                                                           
    return super().request(method, url, *args, **kwargs)                                                                                                                                  
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_vendor/requests/sessions.py", line 542, in request                                                           
    resp = self.send(prep, **send_kwargs)                                                                                                                                                 
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_vendor/requests/sessions.py", line 697, in send                                                              
    r.content                                                                                                                                                                             
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_vendor/requests/models.py", line 836, in content                                                             
    self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''                                                                                                                
  File "/Users/mfansler/miniconda3/envs/foo/lib/python3.9/site-packages/pip/_vendor/requests/models.py", line 769, in generate                                                            
    chunk = self.raw.read(chunk_size)                                                                                                                                                     
AttributeError: 'FileNotFoundError' object has no attribute 'read'

Code of Conduct

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
uranusjrcommented, Jul 30, 2021

file:requirements.txt is not a valid URL. The standard (RFC 8089) mandates the URL must starts with file:// or file: + absolute path. This is neither, so pip treats it as a local relative path and correctly fails.

Feel free to submit a PR for better error messages, but this is not a bug.

1reaction
mfanslercommented, Oct 8, 2021

@DiddiLeija @pradyunsg thanks for addressing this! ❤️

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I raise a FileNotFoundError properly? - Stack Overflow
The first is an error code from the errno module (file not found is always errno.
Read more >
Python FileNotFoundError: [Errno 2] No such file or directory ...
This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check...
Read more >
Python Write to File – Open, Read, Append, and Other File ...
The first parameter of the open() function is file , the absolute or relative path to the file that you are trying to...
Read more >
8. Errors and Exceptions — Python 3.11.1 documentation
When an exception occurs, it may have associated values, also known as the exception's arguments. The presence and types of the arguments depend...
Read more >
chpldoc — Chapel Documentation 1.19 - chapel-lang.
For more information see the Documenting argument, return, and yield ... mode: `int` :throws FileNotFoundError: If :arg:`name` does not correspond to a file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found