devcontainers / devcontainers/images

miniconda image: changing Python version via environment.yml breaks pipx and other packages

Open
#898 1 comment 0 reactions 1 assignee Claimed by @V-Subhankar-infy View on GitHub
Dominant language
Shell
Stars
2.1k
Forks
963
Avg merge
7h 48m
Merged PRs (30d)
11

Description

When using the miniconda image, the `base` environment is re-configured if an `environment.yml` file is found. Installing a different Python version via this conda environment config file breaks `pipx` and other utilities under `/usr/local/py-utils`, making it difficult to fix and causing silent failures (see devcontainers/features#785):

```
$ pipx
Traceback (most recent call last):
File "/usr/local/py-utils/bin/pipx", line 5, in
from pipx.main import cli
ModuleNotFoundError: No module named 'pipx'
```

The README notes the following instructions to fix pipx when a different Python version is installed:

```
RUN pip install --no-cache-dir pipx \
&& pipx uninstall pipx \
&& pipx reinstall-all
```

However this will still fail because the broken pipx binary still has precedence on PATH. A more reliable process, similar to that used by the Python feature itself, is to install a temporary copy of pipx and then use it to fix all the sandboxes:

```
# Copy environment.yml (if found) to a temp location so we update the environment. Also
# copy "noop.txt" so the COPY instruction does not fail if no environment.yml exists.
COPY environment.yml* requirements.txt .devcontainer/noop.txt /tmp/conda-tmp/
RUN if [ -f "/tmp/conda-tmp/environment.yml" ]; then umask 0002 && /opt/conda/bin/conda env update -n base -f /tmp/conda-tmp/environment.yml; fi \
&& rm -rf /tmp/conda-tmp

# reinstall all pipx environments, in case Conda's base Python version changed
RUN export PYTHONUSERBASE=/tmp/pip-tmp PIP_CACHE_DIR=/tmp/pip-tmp/cache \
&& pip3 install --disable-pip-version-check --no-cache-dir --user pipx \
&& /tmp/pip-tmp/bin/pipx reinstall-all \
&& rm -rf /tmp/pip-tmp
```

Example `environment.yml`:
```
name: prompt-flow-env
channels:
- defaults
dependencies:
- python=3.9
- pip
- pip:
- -r requirements.txt
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.