I have this profile settings.json:
{
"terminal.integrated.profiles.osx": {
"python-venv": {
"path": "/opt/homebrew/bin/bash",
"args": [
"-l",
"-c",
"\"/path/to/my/enforce-venv-script.sh\""
],
"icon": "terminal-bash"
}
}
}
And enforce-venv-script.sh:
#!/bin/bash
# Exit on errors
set -e
# Enforce the existence and usage of a Python venv
if [ ! -d ".venv" ]; then
echo "This is a Python project, but no venv was found. Creating one..."
python3 -m venv .venv > /dev/null
echo "Activating venv..."
source .venv/bin/activate
# Hand off execution to a new login shell, or else the terminal
# will simply quit. Don't forget to activate the venv within it!
# What goes here???
fi
I've tried multiple variations of exec bash, with or without -l, -c and source ... commands, but nothing seems to work.
This is all to facilitate the automatic creation of virtual environments for any Python project that I create.
I use this VS Code profile for all new Python projects, and the idea then is to enforce the usage of a virtual environment within that project. So any time a terminal is opened, the script will run and the venv will be created and activated if not already there.
VS Code already activates venvs automatically when I open a terminal and one exists, so the last missing piece of the puzzle is to create one if there isn't one.
EDIT: In response to @Philippe's answer and comments below, here are some more attempts to get this working:
kenny@MACBOOK-PRO:~/Dev/Hobby_Programming/Python/test$ bash -l --rcfile ~/Library/Application\ Support/Code/User/profiles/73486c58/enforce-venv.sh
bash -l --rcfile ~/Library/Application\ Support/Code/User/profiles/73486c58/enforce-venv.sh
bash: --: invalid option
Usage: bash [GNU long option] [option] ...
...more usage info removed for brevity
kenny@MACBOOK-PRO:~/Dev/Hobby_Programming/Python/test$ /opt/homebrew/bin/bash -l -rcfile /Users/kenny/Library/Application\ Support/Code/User/profiles/73486c58/en
force-venv.sh
bash: /Users/kenny/Library/Application: restricted: cannot specify `/' in command names
kenny@MACBOOK-PRO:~/Dev/Hobby_Programming/Python/test$ cd ~/Library/Application\ Support/Code/User/profiles/73486c58/
kenny@MACBOOK-PRO:~/Library/Application Support/Code/User/profiles/73486c58$ /opt/homebrew/bin/bash -l -rcfile enforce-venv.sh
bash: enforce-venv.sh: command not found
kenny@MACBOOK-PRO:~/Library/Application Support/Code/User/profiles/73486c58$ /opt/homebrew/bin/bash -l -rcfile ./enforce-venv.sh
bash: ./enforce-venv.sh: restricted: cannot specify `/' in command names
I have no freaking clue what the heck is different enough about our computers that this is happening to me, since it apparently works for Philippe, yet here I am.
source .venv/bin/activateactivatesvenvonly for this proces, but not for others. After ending script it goes back to original process which doesn't have active venv. Some "workaround" is to use.venv/bin/pythoninstead ofpythonto run your python scripts in your project - and they should run like with active venv.-lat the end ofargs.-larg. to the end stops it from crashing at least, but it seems that's because theenforce-venv-script.shdoesn't run at all (no venv is created) for me. Perhaps if you have the time and patience to deal with a noob like me, we could share screens and I could watch you do this on my PC...?