pythonpipvirtual-environmentflit

Python virtual environments created since mid-August raise `AssertionError` for `python3 -m pip freeze`


On both MacOS 10.15.7 (Python 3.8.6) and Ubuntu 20.04 (Python 3.8.5), I have installed various virtual environments for various Python scripts I am writing (all in Git and on Github). The projects use pyproject.toml for use with flit. I created and populated the environments with modules used in the scripts as follows (where $VENVS is the directory where I keep virtual environments):

$ python3 -m venv $VENVS/csvenv
$ source $VENVS/csvenv/bin/activate
(csvenv) $ python3 -m pip install --upgrade flit
(csvenv) $ flit install -s      # to install script as "editable"

HOWEVER, on both platforms the command python3 -m pip freeze gets different results, when executed in a virtual environment:

ERROR: Exception:
Traceback (most recent call last):
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/cli/base_command.py", line 228, in _main
    status = self.run(options, args)
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/commands/freeze.py", line 101, in run
    for line in freeze(**freeze_kwargs):
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/operations/freeze.py", line 67, in freeze
    req = FrozenRequirement.from_dist(dist)
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/operations/freeze.py", line 257, in from_dist
    req = direct_url_as_pep440_direct_reference(
  File "/Users/tbaker/venvs/dcapenv/lib/python3.8/site-packages/pip/_internal/utils/direct_url_helpers.py", line 49, in direct_url_as_pep440_direct_reference
    assert not direct_url.info.editable
AssertionError

My guess that this depends on when an environment was created is based both on directory timestamps and on examination of builds on Readthedocs, which rebuilds documentation whenever a repo is pushed.

Documentation for a project with an environment created before August 17 has continued to rebuild without interruption, while the documentation for a project with the newer environment has failed to build since August. With the August 17 commit, a requirements.txt file was added.

If I restore that requirements.txt, the RTD build fails with this message:

ERROR: Could not find a version that satisfies the requirement csv2shex==0.2 (from -r docs/requirements.txt (line 16)) (from versions: none)
ERROR: No matching distribution found for csv2shex==0.2 (from -r docs/requirements.txt (line 16))

If I exit the virtual environment with deactivate, execute python3 -m pip freeze >requirements.txt (which works outside of the virtual environment) and push, Readthedocs fails to build with the same error.

pip freeze >requirements.txt does work when I use a virtual environment created in July. However, when I clone that project on Ubuntu and re-create a virtual environment (as above), pip freeze raises the exception. It also fails if I create and populate a new virtual environment for that on MacOS. However, if I create a new requirements.txt file within the virtual environment from July, and push, the documentation builds.

The exception is raised at line 49 of pip's direct_url_helpers.py, which includes the following comment:

    # pip should never reach this point for editables, since
    # pip freeze inspects the editable project location to produce
    # the requirement string
    assert not direct_url.info.editable

I have researched this for many hours. Searches on pip freeze, AssertionError, editable project location, and the like have turned up nothing. The Stackoverflow search "pip freeze assertionerror" yields just 12 results, all but one from 2015, or before, plus one from 2019 about an issue involving Docker.

To summarize, it appears to me that:

However, some things do not add up:

Can anyone out there explain? Is this a problem with python3 -m venv, flit, or something else? Am I at least correct to suspect that the failures of the RTD builds are somehow related to the failures of pip freeze?


Solution

  • The failure of pip freeze was indeed a bug in Pip 2.2. This bug has already been fixed for the upcoming Pip 2.3.