ERROR pip config_error ai_generated true

ERROR: Invalid requirement: 'package==1.0' (line 1 of requirements.txt)

ID: pip/requirements-file-bom

Also available as: JSON · Markdown · 中文
95%Fix Rate
90%Confidence
1Evidence
2024-01-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
pip 23.2 active
pip 24.0 active
Python 3.9 active
Python 3.10 active
Python 3.11 active

Root Cause

The requirements.txt file contains a UTF-8 BOM (Byte Order Mark) character at the beginning, which pip's parser does not strip, causing the first line to be interpreted as an invalid package name.

generic

中文

requirements.txt 文件开头包含 UTF-8 BOM(字节顺序标记)字符,pip 的解析器不会去除该字符,导致第一行被解释为无效的包名。

Official Documentation

https://pip.pypa.io/en/stable/reference/requirements-file-format/

Workarounds

  1. 95% success Remove the BOM using a command-line tool: sed -i '1s/^\xef\xbb\xbf//' requirements.txt (Linux/macOS) or use PowerShell: (Get-Content requirements.txt) | Set-Content -Encoding UTF8NoBOM requirements.txt (Windows). Then retry pip install -r requirements.txt.
    Remove the BOM using a command-line tool: sed -i '1s/^\xef\xbb\xbf//' requirements.txt (Linux/macOS) or use PowerShell: (Get-Content requirements.txt) | Set-Content -Encoding UTF8NoBOM requirements.txt (Windows). Then retry pip install -r requirements.txt.
  2. 92% success Recreate the requirements file without BOM: echo 'package==1.0' > requirements.txt && cat old_requirements.txt | tail -n +2 >> requirements.txt (skip first line with BOM).
    Recreate the requirements file without BOM: echo 'package==1.0' > requirements.txt && cat old_requirements.txt | tail -n +2 >> requirements.txt (skip first line with BOM).
  3. 97% success Convert the file encoding using a Python script: python -c "with open('requirements.txt', 'r', encoding='utf-8-sig') as f: content = f.read(); with open('requirements.txt', 'w', encoding='utf-8') as f: f.write(content)" && pip install -r requirements.txt
    Convert the file encoding using a Python script: python -c "with open('requirements.txt', 'r', encoding='utf-8-sig') as f: content = f.read(); with open('requirements.txt', 'w', encoding='utf-8') as f: f.write(content)" && pip install -r requirements.txt

中文步骤

  1. Remove the BOM using a command-line tool: sed -i '1s/^\xef\xbb\xbf//' requirements.txt (Linux/macOS) or use PowerShell: (Get-Content requirements.txt) | Set-Content -Encoding UTF8NoBOM requirements.txt (Windows). Then retry pip install -r requirements.txt.
  2. Recreate the requirements file without BOM: echo 'package==1.0' > requirements.txt && cat old_requirements.txt | tail -n +2 >> requirements.txt (skip first line with BOM).
  3. Convert the file encoding using a Python script: python -c "with open('requirements.txt', 'r', encoding='utf-8-sig') as f: content = f.read(); with open('requirements.txt', 'w', encoding='utf-8') as f: f.write(content)" && pip install -r requirements.txt

Dead Ends

Common approaches that don't work:

  1. 98% fail

    Adding --no-cache-dir does not fix the BOM character in the file; it only affects pip's cache behavior.

  2. 95% fail

    Using pip install --upgrade pip does not change the file encoding; BOM remains.

  3. 60% fail

    Editing the file manually in a text editor may not remove the BOM if the editor adds it back (e.g., Notepad on Windows).