build icon indicating copy to clipboard operation
build copied to clipboard

Setting environment variables in isolated environment

Open ianjirka opened this issue 3 years ago • 5 comments

How can I modify the isolated environment before dependencies are downloaded in it by the build backend?

I have a scenario where, to build a package I have a dependency on a package contained in an authenticated feed (Azure Pipelines)

I can specify the dependency in pyproject.toml. This works great if the feed is not authenticated.

The problem I run into is the isolated environment produced by python -m build strips any environment variables I have set in the parent (which is expected).

Is there a way to hook so I can modify the isolated environment after it is created but before any of the backend runs? Looking at the backend user interface, all hooks run after dependencies are installed, which is too late.

Thanks much!

ianjirka avatar May 07 '22 01:05 ianjirka

It shouldn't strip all variables, that would be a disaster. Currently the only way to communicate options to the build backend is via environment variables (especially important for binary build backends, but could be needed anywhere). There are a few stripped environment variables that interfere with Python (you can see them in https://github.com/pypa/build/pull/375), but build doesn't strip environment variables - the "isolation" is for Python. You will however need to export variables to get them to affect subprocesses, so maybe that's missing? https://askubuntu.com/questions/205688/whats-the-difference-between-set-export-and-env-and-when-should-i-use-each

henryiii avatar May 07 '22 02:05 henryiii

Ahh, though you are interested in the environment during the pip download. Thought that should still be controllable via environment variables, things like PIP_PREFER_BINARY and such work there.

henryiii avatar May 07 '22 02:05 henryiii

Thank you. I checked to see if the variable I'm using ('PYPI_TOKEN') is in that changelist and it's not. The behavior is definitely different for me between the isolated and non-isolated environment. It should only depend on a single environment variable making it across the boundary - in this case to provide a token to an authenticated feed.

What you see below is most likely caused by the environment variable carrying **** not making it through to the isolated environment. It is exported in my shell, and it works with the --no-isolation tag

[global]
index-url=https://[email protected]/xx/_packaging/yy/pypi/simple/
* Creating venv isolated environment...
* Installing packages in isolated environment... (setuptools>=62.1, foo>=0.0.5)
Looking in indexes: https://****@pkgs.dev.azure.com/xx/_packaging/yy/pypi/simple/ 
WARNING: 401 Error, Credentials not correct for https://pkgs.dev.azure.com/xx/_packaging/yy/pypi/simple/foo/

It could be something different though. Can you recommend a way to debug this? Either a place I can run code to dump various things or a way to leave the environment behind so I can inspect it?

ianjirka avatar May 09 '22 19:05 ianjirka

Comment out these two lines to keep the isolated env. Alternatively you can add a breakpoint() under install.

layday avatar May 09 '22 19:05 layday

@layday perfect, thank you!

ianjirka avatar May 09 '22 19:05 ianjirka