Skip to content

When installing CLIP via pip on Python 3.10+ systems with setuptools …#536

Open
Aadi775 wants to merge 1 commit intoopenai:mainfrom
Aadi775:patch-1
Open

When installing CLIP via pip on Python 3.10+ systems with setuptools …#536
Aadi775 wants to merge 1 commit intoopenai:mainfrom
Aadi775:patch-1

Conversation

@Aadi775
Copy link
Copy Markdown

@Aadi775 Aadi775 commented Mar 22, 2026

…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 #532

What Changed

Before:

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:

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

…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
Copilot AI review requested due to automatic review settings March 22, 2026 10:32
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 reading requirements.txt directly.
  • Improve filtering of blank/comment lines by stripping before checking #.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 11 to +15
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("#")
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +12 to +15
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("#")
Copy link

Copilot AI Mar 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@Aadi775
Copy link
Copy Markdown
Author

Aadi775 commented Mar 22, 2026

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-d50d76daa670286dd6cacf3bcd80b5e4823fc8e1

2. Replace setup.py with the fixed version above, then install:

pip install --no-build-isolation .

Tested on Python 3.10.19, setuptools 82.0.1, Arch Linux.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLIP compilation problem

2 participants