While coding on VSC, I often need to use the terminal within the virtualenv I’m using at the moment.
The problem is: every time I go to "New Terminal", I need to activate the virtualenv I need for that specific session. I normally switch between virtualenvs several times a day, so I am looking for a way to save time.
I must be poorly using VSC features, there must be a better way (profiles? or some extension?) but this is how I am doing it right now:
I have a bash script that activates the virtualenv and then calls VSC. But it doesn’t work all the time, I don’t know exactly why (sometimes "New Terminal" results in no virtualenv at all, and some other times it is still within the last used virtualenv.
The script (see at the end of the message) is associated with a key and path passed as a parameter. For example, I press CTRL+SHIFT+D to execute a script that activates my daily test environment.
So the question is, is there a simple way to open VSC with a given virtualenv already activated so I don’t have to activate it every time I start VSC’s "New Terminal"?
Note I do not want to associate a virtualenv to a specific project, file, workspace or repo, but for a session (by this I mean, every time I open VSC I will use it into one specific virtualenv. If I need a different virtualenv, I just close VSC and re-open it with my script below using a different virtualenv).
Edit 0: The script does not work, because it (wrongly) assumes this: if I activate a virtualenv and then call VSC, VSC ‘new terminal’ will open into the active virtualenv at the time of calling VSC. This is not the case. It doesn’t matter if I call VSC from a terminal that just activated the virtualenv or has no virtualenv activated whatsoever. VSC doesn’t care/know which virtualenv was activated before calling VSC.
This is the script executed when pressing CTRL+SHIFT+D:
#!/bin/bash
echo -------------------------- virtualenv: daily tests --------------------------
echo "changing to path: $1"
cd $1
echo -------------------------- Activate VirtualEnv --------------------------
echo Activate Virtual Environment at:
echo /home/user/.virtualenvs/VirtEnvRG001_daily_tests/bin/activate
source /home/user/.virtualenvs/VirtEnvRG001_daily_tests/bin/activate
echo Wait 1 s
sleep 1
echo Calling VSC...
code $1
2
Answers
Following instructions from wjandrea, I add the solution to my question here.
Thanks to White Owl answer, I added the profiles into the configuration file and it works now as intended. Now I just open the desired profile into the terminal and don't have to activate the virtualenv each time.
In this case I used two virtualenvs (in Linux): 'daily' and 'geo'.
Below is the exact part I added to settings.json:
Yes, it is done with terminal profiles.
you would define a profile like:
Add this into your
settings.json
. And this will force the specified script to run each time a new terminal is opened.