Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activate venv on a per file/folder basis #22687

Closed
thomelane opened this issue Dec 20, 2023 · 1 comment
Closed

Activate venv on a per file/folder basis #22687

thomelane opened this issue Dec 20, 2023 · 1 comment
Assignees
Labels
feature-request Request for new features or functionality triage-needed Needs assignment to the proper sub-team

Comments

@thomelane
Copy link

thomelane commented Dec 20, 2023

I'm currently working in a monorepo that contains multiple Python packages and venvs. e.g.

monorepo/
├── package_a
│   ├── .venv
│   └── main_a.py
└── package_b
    ├── .venv
    └── main_b.py

It would be great to have VSCode automatically change the venv based on the current file, so that things like linting work correctly (i.e. no package import errors). As an example:

  • main_a.py is opened and is active file -> package_a/.venv gets activated.
  • change tab to make main_b.py the active file -> package_b/.venv gets activated.

Working out which is the correct venv for each file could be tricky, but for me a traversal up the file system looking for .venv would work well. WolfiesHorizon.python-auto-venv extension used to do this well but no longer works. Would be great to have this feature in the official extension.

My current workflow every time I open a file from a different sub-packages is:

  • open file
  • copy path to .venv (for that file's package)
  • select interpreter
  • "enter interpreter path"
  • paste in .venv path

So a very manual workflow and pretty disruptive! I copy path because I have a lot of venvs, and it's easier than finding the correct venv from the existing interpreter drop down.

Sorry if this is a duplicate issue, quite hard to search for this one!

@thomelane thomelane added the feature-request Request for new features or functionality label Dec 20, 2023
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Dec 20, 2023
@luabud
Copy link
Member

luabud commented Jan 4, 2024

hey @thomelane! If you open your project as a multi-root workspace in VS Code this should work automatically. We have some documentation in our wiki page with some steps on how to configure it for Python: https://github.com/microsoft/vscode-python/wiki/Mono-Repo-Set%E2%80%90up-Guide#scenario-2-separate-virtual-environments

However if multi-root workspaces don't work for you, one possible workaround is to use the Python Environment Manager extension . It shows you a view of your environments and gives you a way to switch between them as the selected interpreter:
image

We have been tracking feedback regarding how we can improve the mono repo experience at #21204, so I'm going to close this one in favour of that!

@luabud luabud closed this as not planned Won't fix, can't repro, duplicate, stale Jan 4, 2024
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality triage-needed Needs assignment to the proper sub-team
Projects
None yet
Development

No branches or pull requests

3 participants