Comment 7 for bug 1882535

Revision history for this message
Dmitrii Shcherbakov (dmitriis) wrote :

I was trying to disable .pyc files by having PIP_COMPILE=false and PYTHONDONTWRITEBYTECODE=false.

But this isn't possible based on the below.

https://pip.pypa.io/en/stable/user_guide/#environment-variables (pip allows environment variables instead of command-line arguments)
https://docs.python.org/3/using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE (PYTHONDONTWRITEBYTECODE disables writing .pyc files at the interpreter level)

    build-environment: &python-build-environment
      - PIP_COMPILE: 'false' # disable .pyc generation in pip
      - PYTHONDONTWRITEBYTECODE: 'false' # disable .pyc generation by setup.py
      - SOURCE_DATE_EPOCH: '1591640328'

However, I still had some .pyc files created.

The reason is that snapcraft creates a venv which, in turn, uses the `ensurepip` command built into the interpreter (and also adds -I to filter out PYTHON* variables). `ensurepip` has some code to ignore any environment variable prefixed with "PIP_" which includes PIP_COMPILE.

See below:

1) venv used by snapcraft

https://github.com/snapcore/snapcraft/blob/71cebabd8155937fa329c94f7d3559b6b2e723b7/snapcraft/plugins/v2/python.py#L117-L120
"${SNAPCRAFT_PYTHON_INTERPRETER}" -m venv ${SNAPCRAFT_PYTHON_VENV_ARGS} "${SNAPCRAFT_PART_INSTALL}"

For example:
echo $SNAPCRAFT_PYTHON_INTERPRETER $SNAPCRAFT_PYTHON_VENV_ARGS $SNAPCRAFT_PART_INSTALL
python3 /root/parts/cluster/install

python3 -m venv /root/parts/cluster/install

2) ensurepip used by venv with -I option passed.

https://github.com/python/cpython/blob/58ec58a42bece5b2804b178c7a6a7e67328465db/Lib/venv/__init__.py#L291-L298
        cmd = [context.env_exe, '-Im', 'ensurepip', '--upgrade',
                                                    '--default-pip']
        subprocess.check_output(cmd, stderr=subprocess.STDOUT)

https://docs.python.org/3/using/cmdline.html#id2
"-I
... All PYTHON* environment variables are ignored, too. Further restrictions may be imposed to prevent the user from injecting malicious code."

3) ensurepip's filtration of all envars prefixed with "PIP_".

https://github.com/python/cpython/blob/0f5a28f834bdac2da8a04597dc0fc5b71e50da9d/Lib/ensurepip/__init__.py#L47-L56
    keys_to_remove = [k for k in os.environ if k.startswith("PIP_")]
    for k in keys_to_remove:
        del os.environ[k]
https://github.com/python/cpython/blob/0f5a28f834bdac2da8a04597dc0fc5b71e50da9d/Lib/ensurepip/__init__.py#L88-L119