When installing CLIP via pip on Python 3.10+ systems with setuptools …#536
When installing CLIP via pip on Python 3.10+ systems with setuptools …#536Aadi775 wants to merge 1 commit intoopenai:mainfrom
Conversation
…71+, the installation fails with: ``` ModuleNotFoundError: No module named 'pkg_resources' ``` ## Problem When installing CLIP via pip on Python 3.10+ systems with setuptools 71+, the installation fails with: ``` ModuleNotFoundError: No module named 'pkg_resources' ``` pip creates an **isolated build environment** in a temporary directory when building packages from source. In this isolated environment, setuptools is installed fresh — and in newer versions (71+), `pkg_resources` is no longer automatically available as a top-level importable module within that subprocess context, even though setuptools itself is present. The original `setup.py` imported `pkg_resources` at the module level and used `pkg_resources.parse_requirements()` to read `requirements.txt`. Since this code runs inside pip's isolated build subprocess, it fails before the wheel can even be built — making CLIP completely uninstallable from source on modern systems without workarounds. Closes openai#532 ## What Changed **Before:** ```python import pkg_resources install_requires=[ str(r) for r in pkg_resources.parse_requirements( open(os.path.join(os.path.dirname(__file__), "requirements.txt")) ) ] ``` **After:** ```python install_requires=[ stripped for line in open(os.path.join(os.path.dirname(__file__), "requirements.txt")) for stripped in [line.strip()] if stripped and not stripped.startswith("#") ], ``` - Removed the `pkg_resources` import entirely - Replaced `parse_requirements()` with plain Python file I/O - Strips each line before checking for blank/comment — so lines like `" # comment"` are correctly excluded (previous version missed these) - Properly indented inside `install_requires` ## Impact - Fixes installation on any system running setuptools 71+ - Tested on Python 3.10.19, setuptools 82.0.1 - No behavior change for existing users - Removes the only legacy `pkg_resources` dependency from `setup.py` - Makes the build process more robust and future-proof
There was a problem hiding this comment.
Pull request overview
This PR fixes source installs on modern Python/setuptools by removing the pkg_resources dependency from setup.py and parsing requirements.txt using plain file I/O, avoiding failures in pip’s isolated build environment (setuptools 71+).
Changes:
- Remove reliance on
pkg_resources.parse_requirements()by readingrequirements.txtdirectly. - Improve filtering of blank/comment lines by stripping before checking
#.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| install_requires=[ | ||
| line.strip() | ||
| stripped | ||
| for line in open(os.path.join(os.path.dirname(__file__), "requirements.txt")) | ||
| if line.strip() and not line.startswith("#") | ||
| for stripped in [line.strip()] | ||
| if stripped and not stripped.startswith("#") |
There was a problem hiding this comment.
open(..."requirements.txt") is used directly inside the list comprehension without a context manager, so the file handle may remain unclosed until GC. Consider reading the file with a with open(...) (and ideally an explicit encoding) before constructing install_requires to avoid resource warnings and improve robustness on Windows.
| stripped | ||
| for line in open(os.path.join(os.path.dirname(__file__), "requirements.txt")) | ||
| if line.strip() and not line.startswith("#") | ||
| for stripped in [line.strip()] | ||
| if stripped and not stripped.startswith("#") |
There was a problem hiding this comment.
The nested comprehension pattern for stripped in [line.strip()] is an uncommon idiom and makes install_requires harder to read/maintain. It would be clearer to strip once in a small helper (or pre-process the lines) rather than using a single-item list to bind an intermediate value.
Manual Workaround (until merged)If you're hitting this error right now, you can manually install CLIP while this PR is pending: 1. Download and extract the zip: wget "https://github.com/openai/CLIP/archive/d50d76daa670286dd6cacf3bcd80b5e4823fc8e1.zip" -O clip.zip
unzip -q clip.zip
cd CLIP-d50d76daa670286dd6cacf3bcd80b5e4823fc8e12. Replace pip install --no-build-isolation .
|
…71+, the installation fails with:
ModuleNotFoundError: No module named 'pkg_resources'Problem
When installing CLIP via pip on Python 3.10+ systems with setuptools 71+, the installation fails with:
pip creates an isolated build environment in a temporary directory when building packages from source. In this isolated environment, setuptools is installed fresh — and in newer versions (71+),
pkg_resourcesis no longer automatically available as a top-level importable module within that subprocess context, even though setuptools itself is present.The original
setup.pyimportedpkg_resourcesat the module level and usedpkg_resources.parse_requirements()to readrequirements.txt. Since this code runs inside pip's isolated build subprocess, it fails before the wheel can even be built — making CLIP completely uninstallable from source on modern systems without workarounds.Closes #532
What Changed
Before:
After:
pkg_resourcesimport entirelyparse_requirements()with plain Python file I/O" # comment"are correctly excluded (previous version missed these)install_requiresImpact
pkg_resourcesdependency fromsetup.py