pipenv: No module named 'pkg_resources' (hidden lock error, tested)

pipenv install fails with "Locking Failed! Your dependencies could not be resolved", and the hidden cause is ModuleNotFoundError: No module named 'pkg_resources' in a package build. Tested fix: PIP_BUILD_CONSTRAINT with setuptools<81.

1. The error: pipenv install says "dependencies could not be resolved"

Our test package was cx_Oracle 8.3.0. It has no wheel for Python 3.11, so it is built from the sdist, and its setup.py imports pkg_resources. In a fresh project with pipenv 2026.10.2 (pip 26.2.2, fresh caches):

$ pipenv install cx_Oracle==8.3.0
[... trimmed ...]
Upgrading cx_Oracle==8.3.0 in  dependencies.
Building requirements...
Resolving dependencies...
✘ Locking Failed!
ERROR: Failed to build 'cx-oracle' when getting requirements to build wheel
Your dependencies could not be resolved. You likely have a mismatch in your 
sub-dependencies.
You can use $ pipenv run pip install <requirement_name> to bypass this 
mechanism, then run $ pipenv graph to inspect the versions actually installed in
the virtualenv.
Hint: try $ pipenv lock --pre if it is a pre-release dependency.
Hint: try $ pipenv lock --verbose to see the full dependency resolution output.

pipenv reports a resolver "mismatch in your sub-dependencies", but there is no conflict. The words pkg_resources never appear in its output, not even with pipenv lock --verbose, which only adds Getting requirements to build wheel: finished with status 'error'. pipenv also reverted the Pipfile, so cx_Oracle was not installed.

2. See the real error: pipenv run pip install

Run the same requirement through pip inside the pipenv venv to see the hidden traceback:

$ pipenv run pip install cx_Oracle==8.3.0
Collecting cx_Oracle==8.3.0
  Downloading cx_Oracle-8.3.0.tar.gz (363 kB)
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'error'
  error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [19 lines of output]
      [... trimmed ...]
      [end of output]
  
  [... trimmed ...]

pip builds sdists in an isolated build env (/tmp/pip-build-env-...) with the newest setuptools, and setuptools 82.0.0 removed pkg_resources. The venv itself had setuptools 84.2.1, but the build never uses it.

3. Fix: PIP_BUILD_CONSTRAINT (one-off or in .env)

Cap setuptools for build envs only. pipenv runs pip underneath, so pip's PIP_BUILD_CONSTRAINT environment variable reaches the build:

$ echo "setuptools<81" > constraints.txt
$ PIP_BUILD_CONSTRAINT=constraints.txt pipenv install cx_Oracle==8.3.0

With the variable set, the same project locked (✔ Success!), Pipfile.lock got the cx-oracle hashes, and .venv/bin/python -c "import cx_Oracle; print(cx_Oracle.version)" printed 8.3.0.

To make it stick for everyone on the project, put it in the project's .env, which pipenv loads automatically:

$ cat .env
PIP_BUILD_CONSTRAINT=constraints.txt
$ pipenv install cx_Oracle==8.3.0
Loading .env environment variables...
[... trimmed ...]

In a fresh project that also installed and imported 8.3.0. Commit constraints.txt (and .env, if it holds no secrets). Note that PIP_CONSTRAINT does not work here: on pip 26.2.2 it does not reach the isolated build env. For cx_Oracle itself, the long-term fix is oracledb.

Same root cause elsewhere: No module named 'pkg_resources' (general fix) · Poetry · uv.

Frequently asked questions

Why does pipenv say my dependencies could not be resolved?

Here it was not a version conflict. A package's sdist build failed because its setup.py imports pkg_resources, and pipenv reports the failure as a resolution error. Run pipenv run pip install <package> to see the real error.

Why doesn't installing setuptools in the pipenv venv fix it?

pip builds the package in a separate isolated env with the newest setuptools. Use PIP_BUILD_CONSTRAINT with setuptools<81 to cap setuptools in that build env.

Written with AI assistance. Measured on 2026-10-24 on Linux with pipenv 2026.10.2, pip 26.2.2 and CPython 3.11.17, installing cx_Oracle 8.3.0 (sdist) into fresh projects with empty caches. Output pasted unedited except marked trims. PlainTables is our extension; pipenv is free.