pypa / virtualenv

Virtual Python Environment builder
https://virtualenv.pypa.io
MIT License
4.8k stars 1.03k forks source link

deactivate command not available in shell scripts or sub-shells (activate script does not export "deactivate") #2654

Closed geopanther closed 10 months ago

geopanther commented 10 months ago

Issue

When activating an venv environment, the command "deactivate" is only available in the current instance of the shell and not in a shellscript (a shellscript is always executed in sub-shell (separate instance)) or any subshel

Environment

Provide at least:

Output of the virtual environment creation

Make sure to run the creation with -vvv --with-traceback:

$ python3 --with-traceback -vvv -m venv .venv_py3.11
unknown option --with-traceback
usage: /Library/Frameworks/Python.framework/Versions/3.11/Resources/Python.app/Contents/MacOS/Python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
$ python3 -vvv -m venv .venv_py3.11

-> see attached console-output.txt

Steps to reproduce:

$ source .venv_py3.11/bin/activate
$ bash
$ deactivate
bash: deactivate: command not found
$ exit
exit
$ deactivate
$

Suggested solution: Add export -f deactivate to /bin/activate

gaborbernat commented 10 months ago

That will still keep the activation in the first shell as it's not possible to change parent shell envs, so this would cause more confusion than benefit.