This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: os.execl opening a new bash shell doesn't work if initfile/rcfile provided
Type: Stage: resolved
Components: Versions: Python 3.7, Python 3.6, Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Anthony Sottile, siming85
Priority: normal Keywords:

Created on 2019-05-27 16:06 by siming85, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg343654 - (view) Author: Siming Yuan (siming85) * Date: 2019-05-27 16:06
Using os.execl you can open a new bash shell (eg, using python to process some env before opening a new shell.

$ echo $SHLVL
1
$ ~/.pyenv/versions/3.6.4/bin/python3
Python 3.6.4 (default, Feb  5 2018, 16:53:35)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.execl('/bin/bash', '')
$ echo $SHLVL
2

Doing the above works with just /bin/bash no arguments. (notice SHLVL incrementing)

But providing your own custom --init-file or --rcfile, doesn't.
eg - /bin/bashrc --rcfile <path to a venv activate file>

$ echo $SHLVL
1
$ ~/.pyenv/versions/3.6.4/bin/python3
Python 3.6.4 (default, Feb  5 2018, 16:53:35)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.execl('/bin/bash', '--rcfile','/users/me/venv/bin/activate')
$ echo $SHLVL
1

this can be replicated in Python 3.5 to 3.7

can be worked-around if using a wrapper.sh file with:
#! /bin/bash
exec /bin/bash --rcfile /users/me/venv/bin/activate

and running this file in os.execl() instead.
msg343658 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2019-05-27 16:38
You want:

os.execl('/bin/bash', 'bash', '--init-file', ...)


argv[0] is supposed to be the program name, your example puts `--init-file` as the program name
msg343663 - (view) Author: Siming Yuan (siming85) * Date: 2019-05-27 17:18
that works... 
but where does it say arv[0] is the program name?

it seems to be more of a how C works... would be nice if the doc had some examples of that.
msg343668 - (view) Author: Anthony Sottile (Anthony Sottile) * Date: 2019-05-27 18:02
They're thin wrappers around the same C functions -- that's just how C works

it is also documented: https://docs.python.org/3/library/os.html#os.execvpe

> In either case, the arguments to the child process **should start with the name of the command being run**, but this is not enforced.

The pattern that I usually use is

```python
cmd = ('bash', '--init-file', 'foo')
os.execvp(cmd[0], cmd)  # never returns
```
msg343669 - (view) Author: Siming Yuan (siming85) * Date: 2019-05-27 18:19
thank you for the insight and quick response.

thought i hit the weirdest bug ever
History
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81247
2019-05-27 18:43:04xtreaksetstatus: open -> closed
stage: resolved
2019-05-27 18:19:02siming85setresolution: not a bug
messages: + msg343669
2019-05-27 18:02:24Anthony Sottilesetmessages: + msg343668
2019-05-27 17:18:14siming85setmessages: + msg343663
2019-05-27 16:38:07Anthony Sottilesetnosy: + Anthony Sottile
messages: + msg343658
2019-05-27 16:06:15siming85create