Open VSCode within a defined virtualenv

  Kiến thức lập trình

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 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).

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

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT