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

PYTHONDONTWRITEBYTECODE #207

Closed
tianon opened this issue Jun 17, 2017 · 7 comments · Fixed by #700
Closed

PYTHONDONTWRITEBYTECODE #207

tianon opened this issue Jun 17, 2017 · 7 comments · Fixed by #700
Labels

Comments

@tianon
Copy link
Member

tianon commented Jun 17, 2017

We should play with exporting PYTHONDONTWRITEBYTECODE instead of deleting the pyc files after the fact!

See also https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE and https://docs.python.org/2/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE.

@tianon
Copy link
Member Author

tianon commented Jun 23, 2017

Played with this a little bit, and it looks like the Python build scripts create *.pyc files unconditionally.

Additionally, get-pip.py creates *.pyc files too, which I'm guessing are because it's pulling in pre-compiled wheels, so the *.pyc files are coming in as part of what's being downloaded/installed, not being generated by Python or pip.

@xavfernandez
Copy link

FWIW *.pyc are generated by pip but you can provide --no-compile to get-pip.py (and/or pip) and it shouldn't create the .pyc files.

@tianon
Copy link
Member Author

tianon commented Sep 27, 2019

Indeed! I did some experimentation:

diff --git a/3.7/alpine3.10/Dockerfile b/3.7/alpine3.10/Dockerfile
index 05dfb5f..63c4bc2 100644
--- a/3.7/alpine3.10/Dockerfile
+++ b/3.7/alpine3.10/Dockerfile
@@ -152,14 +152,16 @@ RUN set -ex; \
        wget -O get-pip.py "$PYTHON_GET_PIP_URL"; \
        echo "$PYTHON_GET_PIP_SHA256 *get-pip.py" | sha256sum -c -; \
        \
+       export PYTHONDONTWRITEBYTECODE=1; \
        python get-pip.py \
                --disable-pip-version-check \
                --no-cache-dir \
+               --no-compile \
                "pip==$PYTHON_PIP_VERSION" \
        ; \
        pip --version; \
        \
-       find /usr/local -depth \
+       echo find /usr/local -depth \
                \( \
                        \( -type d -a \( -name test -o -name tests \) \) \
                        -o \

Running find /usr/local -name '*.pyc' in the resulting image came up empty, which is interesting. 😄

@tianon
Copy link
Member Author

tianon commented Feb 18, 2022

Coming back to this with fresh eyes, I'm not convinced adding this makes our Dockerfile more clear (or really much/any more efficient), especially since we'd probably still want a test just to make sure we didn't somehow regress. 🙈 As such, I'm leaning towards closing this. 🙈

@imomaliev
Copy link

I think this could greatly simplify Dockerfile's and slightly improve build times. Also, I should note that many developers copy some parts of official Dockerfile's and using best practices would be a good thing. Regarding testing for regression, IMHO that there probably should already be a test to check that images don't contain py[co] files in case somebody breaks cleanup.

@tianon
Copy link
Member Author

tianon commented Feb 18, 2022

Yeah, that's fair -- I suppose it would make more sense to make that one of our post-build integration tests instead of part of the Dockerfile itself.

Edit: ... and merged in docker-library/official-images#11896 😄

@imomaliev
Copy link

@tianon Great news. Thank you for all the work)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants