-
Notifications
You must be signed in to change notification settings - Fork 181
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
Using pip without venv or conda env will stop working soon #670
Comments
One may override this using Cross-references: |
That is not the recommended way, though. See python/cpython#102134 (comment) for alternatives. |
|
Hi, thank you for opening this! I ran into this in another repository (rocker-org/devcontainer-features#162) and it is a very headache. The main problem I am aware of is that when you install Python with pyenv or similar, it is generally associated with a user. On the other hand, the images in this repository expect both root and non-root users to be able to run the same commands....... So I vote to use |
(For many reasons) I prefer a separate Python installation from the one bundled with the OS. That is why I am creating customised builds of the official Python Docker Hub Image: https://gitlab.b-data.ch/python/psi Docker images: https://gitlab.b-data.ch/python/psi/container_registry
Usage: ARG BASE_IMAGE=ubuntu
ARG BASE_IMAGE_TAG=22.04
ARG PYTHON_VERSION=3.11.4
FROM glcr.b-data.ch/python/psi/${PYTHON_VERSION}/${BASE_IMAGE}:${BASE_IMAGE_TAG} as psi
FROM ${BASE_IMAGE}:${BASE_IMAGE_TAG}
ARG DEBIAN_FRONTEND=noninteractive
ARG BASE_IMAGE
ARG BASE_IMAGE_TAG
ARG PYTHON_VERSION
ENV BASE_IMAGE=${BASE_IMAGE}:${BASE_IMAGE_TAG} \
PYTHON_VERSION=${PYTHON_VERSION}
COPY --from=psi /usr/local /usr/local
RUN apt-get update \
## Python: Runtime dependencies
&& apt-get -y install --no-install-recommends \
ca-certificates \
netbase \
tzdata \
## Clean up
&& rm -rf /var/lib/apt/lists/* |
This is true for pyenv, but pyenv is a completely different project solving completely different issues than a virtualenv. I agree pyenv should not be used here, as it adds unnecessary complexity for no positive use here. A virtualenv ships built in with python (as part of the |
Hmmm, I guess we expect to be able to use jupyter commands everywhere here, but is venv, which requires activation, a solution? |
You don't need activation to use a venv, just setting FROM ubuntu:23.04
RUN apt-get update && apt-get -y install python3 python3-pip python3-venv
# Setup virtualenv in this path
ENV VIRTUAL_ENV=/opt/venv
# Put the bin/ dir of the venv before other items in path, so `python` refers to this
# No activation script is needed, just this PATH setting is enough
ENV PATH=${VIRTUAL_ENV}/bin:${PATH}
RUN python3 -m venv ${VIRTUAL_ENV}
RUN pip install jupyterlab Note two things here:
The |
@yuvipanda thanks very much for this. I agree with @eitsupi 's point that it feels weird to worry about clobbering system python in a container setting, but overall I think it makes sense to align python use in Rocker with idiomatic use in other 'data science container' ecosystems (including ones you maintain!). Python development is clearly going this way, and we already see friction with tools like RStudio's reticulate which has a similar reticence about system python because of course it can't assume it's in a containerized environment. Putting I do like that you're using |
Thanks, @cboettig! I've currently just come to accept the benefits of using conda to get python, to fully decouple it from system python and get different versions. In my example on how to inherit from rocker images, I use mambaforge to get python from conda-forge (https://jupyterhub-image.guide/rocker.html#step-3-construct-your-dockerfile-to-add-python), and it has worked surprisingly well. Despite being a massive fan of apt, and wishing for less fragmentation in the packaing communtities, I do still recommend mamba+conda-forge as the 'right' solution for ease of maintenance of scientific python involved Docker images. See berkeley-dsep-infra/datahub#2934 (comment) for a detailed example, particularly involving geo based packages. However, if you don't want to use conda-forge here, I'll happily implement the venv solution as well. LMK which way you'd like to go, and I'll provide a PR. |
IMHO Python-wise, a separate Python installation is the cleanest solution. Conda/Mamba is just another unnecessary dependency. |
@yuvipanda Thank you for the detailed explanation! Having said that, I actually do not use Python installed by this script, so I will leave the final decision to @cboettig. For users who want to use Python in earnest, I recommend that they install their favorite version of Python with micromamba or rye, not the one installed by this script...... |
I am really far from being a typical Python user (and not that much of Python user in the first place) but if I were to vote here it would be to continue with what has been done and rely on the package manager's python as much as we can ... while also avoid |
The only thing I would recommend against is
Yep that makes sense! |
ok, shall we go ahead with apt-based install of python3-venv and a shared default venv of |
Does this mean that the |
- Continues to use python and venv from Ubuntu LTS repositories, so they are supported as with everything else that is gotten from apt (see rocker-org#670 (comment)) - Doesn't currently change any permissions, so present behavior is preserved. However, in the future, we should probably change ownership so end users can install packages in there at runtime (see rocker-org#670 (comment)) - Sets the VIRTUAL_ENV environment variable to path of the venv we create. This is what the `source activate` script does, and Reticulate also looks for this to discover which python to use (see point 4 of https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery) - Sets up PATH appropriately, so python and python3 refer to what is in our venv. This, along with the previous step, ensures same behavior as users typing `source ${VIRTUAL_ENV}/bin/activate` without actually having to do that, preserving end user behavioral semantics. - Remove the explicit symlink of python3 -> python, as venv handles this automatically. Decisions to be made: - Where do we set the appropriate env variables (VIRTUAL_ENV and PATH)? They need to be set for `install_python.sh` to work correctly. I've set them in the binder image for now, but it should probably be set on a more base image. This is a no-op if `install-python.sh` is not called anywhere. Ref rocker-org#670
- Continues to use python and venv from Ubuntu LTS repositories, so they are supported as with everything else that is gotten from apt (see rocker-org#670 (comment)) - Doesn't currently change any permissions, so present behavior is preserved. However, in the future, we should probably change ownership so end users can install packages in there at runtime (see rocker-org#670 (comment)) - Sets the VIRTUAL_ENV environment variable to path of the venv we create. This is what the `source activate` script does, and Reticulate also looks for this to discover which python to use (see point 4 of https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery) - Sets up PATH appropriately, so python and python3 refer to what is in our venv. This, along with the previous step, ensures same behavior as users typing `source ${VIRTUAL_ENV}/bin/activate` without actually having to do that, preserving end user behavioral semantics. - Remove the explicit symlink of python3 -> python, as venv handles this automatically. Decisions to be made: - Where do we set the appropriate env variables (VIRTUAL_ENV and PATH)? They need to be set for `install_python.sh` to work correctly. I've set them in the binder image for now, but it should probably be set on a more base image. This is a no-op if `install-python.sh` is not called anywhere. Ref rocker-org#670
- Continues to use python and venv from Ubuntu LTS repositories, so they are supported as with everything else that is gotten from apt (see rocker-org#670 (comment)) - Doesn't currently change any permissions, so present behavior is preserved. However, in the future, we should probably change ownership so end users can install packages in there at runtime (see rocker-org#670 (comment)) - Sets the VIRTUAL_ENV environment variable to path of the venv we create. This is what the `source activate` script does, and Reticulate also looks for this to discover which python to use (see point 4 of https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery) - Sets up PATH appropriately, so python and python3 refer to what is in our venv. This, along with the previous step, ensures same behavior as users typing `source ${VIRTUAL_ENV}/bin/activate` without actually having to do that, preserving end user behavioral semantics. - Remove the explicit symlink of python3 -> python, as venv handles this automatically. Decisions to be made: - Where do we set the appropriate env variables (VIRTUAL_ENV and PATH)? They need to be set for `install_python.sh` to work correctly. I've set them in the binder image for now, but it should probably be set on a more base image. This is a no-op if `install-python.sh` is not called anywhere. Ref rocker-org#670
- Continues to use python and venv from Ubuntu LTS repositories, so they are supported as with everything else that is gotten from apt (see rocker-org#670 (comment)) - Doesn't currently change any permissions, so present behavior is preserved. However, in the future, we should probably change ownership so end users can install packages in there at runtime (see rocker-org#670 (comment)) - Sets the VIRTUAL_ENV environment variable to path of the venv we create. This is what the `source activate` script does, and Reticulate also looks for this to discover which python to use (see point 4 of https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery) - Sets up PATH appropriately, so python and python3 refer to what is in our venv. This, along with the previous step, ensures same behavior as users typing `source ${VIRTUAL_ENV}/bin/activate` without actually having to do that, preserving end user behavioral semantics. - Remove the explicit symlink of python3 -> python, as venv handles this automatically. Decisions to be made: - Where do we set the appropriate env variables (VIRTUAL_ENV and PATH)? They need to be set for `install_python.sh` to work correctly. I've set them in the binder image for now, but it should probably be set on a more base image. This is a no-op if `install-python.sh` is not called anywhere. Ref rocker-org#670
- Continues to use python and venv from Ubuntu LTS repositories, so they are supported as with everything else that is gotten from apt (see #670 (comment)) - Doesn't currently change any permissions, so present behavior is preserved. However, in the future, we should probably change ownership so end users can install packages in there at runtime (see #670 (comment)) - Sets the VIRTUAL_ENV environment variable to path of the venv we create. This is what the `source activate` script does, and Reticulate also looks for this to discover which python to use (see point 4 of https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery) - Sets up PATH appropriately, so python and python3 refer to what is in our venv. This, along with the previous step, ensures same behavior as users typing `source ${VIRTUAL_ENV}/bin/activate` without actually having to do that, preserving end user behavioral semantics. See #670 (comment) - RStudio is also told about new `PATH` and `VIRTUAL_ENV`, using the same pattern as `install_texlive.sh` - Remove the explicit symlink of python3 -> python, as venv handles this automatically. - `install_python.sh` now needs to be `source`d, following same pattern as `install_texlive.sh` Decisions to be made: - Where do we set the appropriate env variables (VIRTUAL_ENV and PATH)? They need to be set for `install_python.sh` to work correctly. I've set them in the binder image for now, but it should probably be set on a more base image. This is a no-op if `install-python.sh` is not called anywhere. TODO: - [x] Update `NEWS` (can be done once everything else is finalized) Ref #670 --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
I think this can now be closed, given #718 has been merged. Thanks for the thoughtful review, everyone! |
https://peps.python.org/pep-0668/ was adopted 2 years ago, and it's finally rolling around to distros downstream. The upshot is that just using
pip
with system python will no longer work. With the recently released ubuntu:23.04 (or debian:bookworm), if you try this:You'll get the following error:
https://pythonspeed.com/articles/externally-managed-environment-pep-668/ has more useful information.
The
install_jupyter.sh
script currently does this (rocker-versioned2/scripts/install_jupyter.sh
Line 32 in a00b24f
There are two options going forward to prevent breaking:
Am happy to put some effort into this, whichever option y'all wanna go with!
The text was updated successfully, but these errors were encountered: