Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
stewartadam opened this issue Dec 21, 2023 · 0 comments

Comments

@stewartadam
Copy link

stewartadam commented Dec 21, 2023

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

No branches or pull requests

1 participant