I want to use Pants to package some Python code an...
# general
a
I want to use Pants to package some Python code and wrap it into a PEX-file. When this PEX-file runs I want it to inherit all environment variables from the system, is this possible?
r
There is an
env
option that you can pass to
pex_binary
as mentioned in the docs Although you will have to list all of the env vars from the system that you want.
a
Yeah, so there's no way to make it behave as if it's a normal binary inheriting the entire environment, then.
I'll be able to make do without it, but it would have been very nice to have.
r
I don't think so. I suppose that's by design. Maybe someone else can chime in with better solution.
e
PEXes do inherit the env where they run. Just try it. Run
pants package ...
to have Pants build your PEX and then run the PEX file. Now, when Pants runs your PEX file (not you), this can change. In certain scenarios Pants hides env vars.
a
That's not what I'm seeing here. I have to do: SOME_VAR=some_value ./my-pex in order for the environment variable to be present. This is in a Gitlab CI pipeline where the variable is defined using
variables:
.
e
Well, not sure what to tell you:
Copy code
jsirois@siroisdesign:~$ echo 'import colors, os; print(colors.yellow(os.environ["FOO"]))' > exe.py
jsirois@siroisdesign:~$ pex ansicolors --exe exe.py -o example.pex
jsirois@siroisdesign:~$ pex -V
2.1.137
(pex.venv) jsirois@siroisdesign:~$ ./example.pex
Traceback (most recent call last):
  File "/usr/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/__main__.py", line 106, in <module>
    bootstrap_pex(__entry_point__, execute=__execute__, venv_dir=__venv_dir__)
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/.bootstrap/pex/pex_bootstrapper.py", line 618, in bootstrap_pex
    pex.PEX(entry_point).execute()
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/.bootstrap/pex/pex.py", line 560, in execute
    sys.exit(self._wrap_coverage(self._wrap_profiling, self._execute))
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/.bootstrap/pex/pex.py", line 467, in _wrap_coverage
    return runner(*args)
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/.bootstrap/pex/pex.py", line 498, in _wrap_profiling
    return runner(*args)
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/.bootstrap/pex/pex.py", line 603, in _execute
    return self.execute_entry(
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/.bootstrap/pex/pex.py", line 800, in execute_entry
    return self.execute_module(entry_point.module)
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/.bootstrap/pex/pex.py", line 808, in execute_module
    runpy.run_module(module_name, run_name="__main__", alter_sys=True)
  File "/usr/lib/python3.9/runpy.py", line 210, in run_module
    return _run_module_code(code, init_globals, run_name, mod_spec)
  File "/usr/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/usr/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/jsirois/.pex/unzipped_pexes/6a3a805ada20ebeb98566f705c22fd54a2fe7f9c/__pex_executable__.py", line 1, in <module>
    import colors, os; print(colors.yellow(os.environ["FOO"]))
  File "/usr/lib/python3.9/os.py", line 679, in __getitem__
    raise KeyError(key) from None
KeyError: 'FOO'
(pex.venv) jsirois@siroisdesign:~$ FOO=bar ./example.pex
bar
(pex.venv) jsirois@siroisdesign:~$
The second success run with FOO defined prints in yellow.
a
I'll keep digging, Josh. Your example is pretty clear, so something is definitely funky on my end then. Thank you!