Repository navigation
Description
Environment
OS: Ubuntu 16.04 (nvidia/cuda:8.0-cudnn6-devel)
Python version: 3.6.5
Conda version:
conda 4.5.10
Pip version:
pip 18.0
Description
Pip install stopped working during docker build of a complex docker container (based on Kaggle/docker-python) and it cannot be repaired by simple methods such as reinstalling / downgrading with pip itself, with conda or a Miniconda script, unless you change the default installation location (/opt/conda) to an alternative folder when using Minconda script to re-install pip (effectively moving all subsequent libraries installs with pip to a separate location). Note that pip uninstall still works (and can be used to remove pip - apparently not effectively enough).
Expected behavior
pip install should be working even in these conditions, i.e. . pip install scipy should succeed regardless of the complexity of the installation.
How to Reproduce
I managed to make it reproducible by providing a functional but only partially completed docker container produced by docker build just before the error occured (I verified that it is reproducible on two different servers).
The bug can be reproduced by executing pip install in bash in this archived docker image that captures this bug (no login requried):
docker pull mirekphd/tst-ml-gpu-py36-cuda8-failing:001766c55ca8
docker run -it --rm --name test -u 0 mirekphd/tst-ml-gpu-py36-cuda8-failing:001766c55ca8 bash
pip install scipy
Possible causes
The last completed operation during build and hence the possible culprit was Jupyter Notebook (or Notebook Extensions) installation with conda (the only installer that makes Jupyter Notebook Extensions work in Jupyter Notebook - pip installation made earlier did not suffice). When I moved Jupyter (Notebook, Lab and Notebook Extensions) installation to the end of the build process (i.e. after all other libraries), the build completed and all libraries can be loaded (from a single standard location in /opt/conda). However, Jupyter section was once again the last one to succeed with pip install, and pip still gets broken, raising the same error (which can be even considered a feature not a bug as it helps maintain library / ML models reproducibility:). I also notice that the same error is raised by pip show and pipdeptree (e.g. !pipdeptree --reverse --packages xgboost) but not by pip freeze.
An intial workaround
A complex but universal workaround I found was to change the pip installation location into an alternative folder when using Miniconda script to re-install pip.
Dockerfile excerpt that repairs this issue and allows the build to succeed:
# ARG MINICONDA_VER=4.5.4 # (same as defined above)
# ARG PIP_VER=18.0 # (same as defined above)
# define alternative pip location
ENV PIP_RESCUE_FOLDER=/tmp/opt/conda
RUN mkdir -p ${PIP_RESCUE_FOLDER} && cd ${PIP_RESCUE_FOLDER} && \
# download Miniconda installation script including pip binary
wget https://repo.continuum.io/miniconda/Miniconda3-${MINICONDA_VER}-Linux-x86_64.sh && \
# note we install pip (and entire Miniconda) in the secondary location by specifying the "prefix folder",
# using parameter -p (hence PREFIX=/tmp/opt/conda and not /opt/conda as it is usually done)
/bin/bash Miniconda3-${MINICONDA_VER}-Linux-x86_64.sh -f -b -p ${PIP_RESCUE_FOLDER} && \
# /tmp/opt/conda/bin/pip --version
# pip 10.0.1 from /tmp/opt/conda/lib/python3.6/site-packages/pip (python 3.6)
# update secondary pip to the desired version
/tmp/opt/conda/bin/pip install --upgrade pip==${PIP_VER} && \
# overwrite pip in primary location with the one just installed into secondary one
cp /tmp/opt/conda/bin/pip /opt/conda/bin && \
cp -R /tmp/opt/conda/lib/python3.6/site-packages/pip /opt/conda/lib/python3.6/site-packages
# to make the libraries installed by secondary pip (into the pip rescue folder) work in python
# we need to add this secondary bin folder to PATH
ENV PATH=$PIP_RESCUE_FOLDER/bin:$PATH
# to verify it works:
# $python
# >>> import openshift as oc
# >>> oc
# <module 'openshift' from '/tmp/opt/conda/lib/python3.6/site-packages/openshift/__init__.py'>
Output
The expected error message is:
root@2dba6ccb6b94:/# pip install scipy --no-cache-dir
Requirement already satisfied: scipy in /opt/conda/lib/python3.6/site-packages (1.1.0)
Requirement already satisfied: numpy>=1.8.2 in /opt/conda/lib/python3.6/site-packages (from scipy) (1.15.2)
Exception:
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2869, in _dep_map
return self.__dep_map
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2663, in __getattr__
raise AttributeError(attr)
AttributeError: _DistInfoDistribution__dep_map
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/packaging/requirements.py", line 93, in __init__
req = REQUIREMENT.parseString(requirement_string)
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pyparsing.py", line 1632, in parseString
raise exc
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pyparsing.py", line 1622, in parseString
loc, tokens = self._parse( instring, 0 )
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pyparsing.py", line 1379, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pyparsing.py", line 3395, in parseImpl
loc, exprtokens = e._parse( instring, loc, doActions )
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pyparsing.py", line 1383, in _parseNoCache
loc,tokens = self.parseImpl( instring, preloc, doActions )
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pyparsing.py", line 3183, in parseImpl
raise ParseException(instring, loc, self.errmsg, self)
pip._vendor.pyparsing.ParseException: Expected stringEnd (at char 33), (line:1, col:34)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2949, in __init__
super(Requirement, self).__init__(requirement_string)
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/packaging/requirements.py", line 97, in __init__
requirement_string[e.loc:e.loc + 8]))
pip._vendor.packaging.requirements.InvalidRequirement: Invalid requirement, parse error at "'; extra '"
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/pip/_internal/basecommand.py", line 141, in main
status = self.run(options, args)
File "/opt/conda/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 330, in run
self._warn_about_conflicts(to_install)
File "/opt/conda/lib/python3.6/site-packages/pip/_internal/commands/install.py", line 456, in _warn_about_conflicts
package_set, _dep_info = check_install_conflicts(to_install)
File "/opt/conda/lib/python3.6/site-packages/pip/_internal/operations/check.py", line 98, in check_install_conflicts
package_set = create_package_set_from_installed()
File "/opt/conda/lib/python3.6/site-packages/pip/_internal/operations/check.py", line 41, in create_package_set_from_installed
package_set[name] = PackageDetails(dist.version, dist.requires())
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2607, in requires
dm = self._dep_map
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2871, in _dep_map
self.__dep_map = self._compute_dependencies()
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2881, in _compute_dependencies
reqs.extend(parse_requirements(req))
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2942, in parse_requirements
yield Requirement(line)
File "/opt/conda/lib/python3.6/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2951, in __init__
raise RequirementParseError(str(e))
pip._vendor.pkg_resources.RequirementParseError: Invalid requirement, parse error at "'; extra '"
root@2dba6ccb6b94:/#
Activity
[-]_pip install_ broken beyond normal repair (AttributeError: _DistInfoDistribution__dep_map)[/-][+]_pip install_ broken beyond normal repair by Jupyter Notebook or its Extensions (AttributeError: _DistInfoDistribution__dep_map)[/+]mirekphd commentedon Oct 1, 2018
The culprit that broke pip install was not Jupyter Notebook itself (already installed successfully using pip earlier in the build sequence), but its Extensions / Configurator installed with conda (!) in the versions specified below:
Note that not installing these extensions (which prevents pip from breaking down) is not the same as simply uninstalling them (which does not work - if these extensions are installed even once, pip gets broken beyond repair). If that breakage happened outside boundaries of an isolated container environment, it would be pretty serious.
[-]_pip install_ broken beyond normal repair by Jupyter Notebook or its Extensions (AttributeError: _DistInfoDistribution__dep_map)[/-][+]_pip install_ broken beyond normal repair by Jupyter Notebook Extensions (AttributeError: _DistInfoDistribution__dep_map)[/+]takluyver commentedon Oct 2, 2018
It looks like #3704 fixed a similar issue for
pip freeze, but this error is coming from the new functionality that checks all installed packages on everypip install. I think a similar try/except is needed around this line:pip/src/pip/_internal/operations/check.py
Line 41 in b6bbabe
I'd also say that the whole check operation should be try/excepted, because bugs in that shouldn't cause
pip installto blow up. I think this would be the relevant line:pip/src/pip/_internal/commands/install.py
Line 475 in b6bbabe
I think it's a bug in Flit causing at least some of these problems - sorry about that, and hopefully I've got it fixed now. But clearly an installed package with broken metadata shouldn't break pip like this.
takluyver commentedon Oct 2, 2018
I've had a go at fixing this in #5842 .
astrojuanlu commentedon Oct 2, 2018
I've found this particular issue in older versions of pip as well, including 10 and 9.
PRJ: dont test Python 3.5 as long as pip3 is broken (see pypa/pip#5839)
Update jupyter.sh to set testpath<0.4
Update jupyter.sh to set testpath<0.4 (#362)
mirekphd commentedon Oct 6, 2018
An update: in my complex python environment upgrading to the the latest versions of all libraries that appeared since the original bug report (including pip 18.0->18.1 and testpath 0.4 -> 0.42) did not help - installing latest Jupyter+extensions+configurator breaks down pip beyond normal repair, although error messages are different this time. In simple environments such as jupyter/base-notebook this error would go unnoticed.
And it's not testpath's fault. If jupyter alone is installed (without extensions and configurator), than no version of testpath can break it (tried versions from 0.3.1. to 0.4.2).
More details
takluyver commentedon Oct 6, 2018
The error message there (
EnvironmentError: [Errno 2] No such file or directory: '/opt/conda/lib/python3.6/site-packages/mistune-0.8.3.dist-info/METADATA') looks different enough that I'd suggest filing a separate issue for it. Mistune isn't using Flit, so hopefully it's not me breaking stuff this time. ;-)takluyver commentedon Oct 6, 2018
I have seen a similar error to that, with the METADATA file missing from
six; it was a problem affecting CI builds, but it seemed to go away when I upgraded pip.mirekphd commentedon Oct 6, 2018
Maybe this one should be filled with conda? After all, isn't it their installer's failure to roll back that cripples pip?
And a simple solution exists now, because we know exactly which package's folder to remove:
And if we break pip again (with the usual "conda install -y -c conda-forge jupyter jupyter_contrib_nbextensions jupyter_nbextensions_configurator"):
..we (i.e. typically root) can remove the entire folder with the newly broken package: ipywidgets, and the removal repairs pip once again:
rodrev commentedon Oct 6, 2018
It's working for me now (essentially using workaround by @mirekphd).
I was having the same problem upgrading pip packages in a conda environment.
base conda environment
~/miniconda3This is a base conda environment with just the minimal packages to install conda, plus conda's pip (10.0.1) and pip's pip (18.0 now 18.1).
ai conda environment
~/miniconda3/envs/aiThis has a ton of packages and just a handful of pip packages. It also had it's own pip (This may have been a problem) also of version 18.0 now removed.
Solution:
double check that it is gone.
ls ~/miniconda3/envs/ai/bin | grep pippip install still wasn't working yet. I realized that my base conda environment was not active while in my ai conda environment and I was using my system /usr/bin/pip after step 2.
Now your $PATH should begin with:
~/miniconda3/envs/ai/bin:~/miniconda3/binAnd use pip 18.1 from your base environment, ~/miniconda3/bin.
sudo pip install --upgrade <package>OK.
pip installshould now work under these conditions.P.S. The set of pip packages I have are different, so the problem is likely independent of jupyter notebook extensions, etc.
working update
original failed update
jjhelmus commentedon Oct 6, 2018
Can you see if this issue is still occurring with the latest packages in the conda ecosystem?
The
testpath0.4.1 conda packages in both thedefaultsandconda-forgechannels contained a METADATA file with an invalid Required-Dist line that pkg_resources was not able to parse. Both channels now have corrected packages.mirekphd commentedon Oct 7, 2018
That problem with pkg_resources seems to be gone indeed. But that does not stop Jupyter Extensions (conda installed) from breaking pip in my Kaggle-like environment, like this...
mirekphd commentedon Oct 7, 2018
But a workaround is to manually remove those folders reported in sequence by pip in its [Errno 2] error message (there are two such libs to purge: mistune 0.8.3 and ipywidgets 7.4.2, both from the conda-forge channel and installed together with jupyter_contrib_nbextensions and jupyter_nbextensions_configurator). Presumably pip should be immunized from being broken by such malformed packages as well?
takluyver commentedon Oct 7, 2018
Re: reporting it on conda - it's certainly an issue that the post-link script fails. But I'd say the bigger issue is that pip should handle the situation a bit more gracefully.
mirekphd commentedon Oct 7, 2018
Maybe moving these folders to a temporary "quarantine" folder would be acceptable?
astrojuanlu commentedon Oct 8, 2018
Is this related to jupyter/notebook#2605 in any way?
mirekphd commentedon Oct 8, 2018
I think so, only the solution offered was not general enough to work in more complex cases like this one. I cannot reproduce their case, but can quickly prove their solution does not work here. Docker has no equals when it comes to reproducing bugs...
astrojuanlu commentedon Oct 9, 2018
Apart from the situation with conda, we were hit by this issue when trying to use a PEP 508 URL requirement in an
extras_requiresection (see #5788 (comment)). To debug this issue, we had to insert apdb.set_trace()inpip/_vendor/packaging/requirements.py, so I totally agree with @takluyver here:Perhaps it should print the full problematic line and hide the useless
_DistInfoDistribution__dep_maperror?Merge #1723
Scheduled monthly dependency update for February (#22)
pradyunsg commentedon Jul 20, 2019
This was fixed in #5842.