2

I have 2 problems. Here was my set-up. I have all of my respositories in ~/dev/, and I had all of my Python virtualenvs in ~/virtualenvs/. This worked OK, but one annoying thing was that it was difficult to switch between virtualenvs, because for some reason VSCode doesn't automatically see where all of my virtualenvs are (which were in ~/virtualenvs/), and I had to enter the full path to the Python executable of my virtualenv every single time. So my first question is, is there a way to tell VSCode where to go and search for virtualenvs?

In order to solve this I started making my virtualenvs inside the repositories themselves, correspondingly. So, in ~/dev/project_1 I'd have ~/dev/project_1/project_1.virtualenv, and inside ~/dev/project_2 I'd have ~/dev/project_2/project_2.virtualenv, and so forth, and both ~/dev/project_1 and ~/dev/project_2 and other projects will be in my workspace. However, VSCode now starts trying to analyse all of the python modules inside those virtualenvs, tens of thousands of files. This is despite my trying to stop it from doing it using Pylance exclude settings. So my second question is, how do I stop VSCode from analysing my virtualenvs?

Answers to either or both of my problems would be much appreciated.

EDIT (the answer):

It was my mistake. I have a pyrightconfig.json file that overrides any code analysis setting in VSCode. All I had to do is to add .venv to the ignore-list in pyrightconfig.json and everything works fine now.

3
  • Have you tried naming your venv dirs something standard like ~/dev/project1/.venv? Commented Mar 5, 2025 at 12:30
  • You first activate the venv for a shell, then you you start VS Code. Important: You must start it from the shell you activated it in (i.e. code path/to/code), so that it inherits the venv's setup. Commented Mar 5, 2025 at 16:01
  • 1
    BTW: Don't ask two questions in one here, in particular not when they are as loosely related as your two. Commented Mar 5, 2025 at 16:02

2 Answers 2

2

If you create your virtual env inside of your repository with a name like .venv, e.g.

python3.13 -m venv .venv
source .venv/bin/activate
python3.13 -m pip install -r requirements.txt...
# etc

... then the Python integration in VSCode will see this and pop up a toast asking you if you want use it.

There are some other default-discovered virtualenv names but this one works well for us.

Sign up to request clarification or add additional context in comments.

Comments

1

Your project's .vscode\\settings.json You can use python.defaultInterpreterPath:

"python.defaultInterpreterPath": "C:\\dist\\venvs\\myrepo\\Scripts\\python.exe"

My venv was created with:

python.exe -m venv c:\\dist\\venvs\\myrepo
c:\\dist\\venvs\\myrepo\\Scripts\\Activate.ps1

Same for uv and pyenv (mac/linux), just set it to the virtual environment's interpreter python binary

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.