How to wait for virtual environment to be activated before issuing commands in a terminal? #18978
-
I've got an extension that executes commands in a terminal. It relies on the Python extension to activate the correct virtual environment in the terminal so that the command (which is installed into the venv) is available. However, I've found that when I just run:
Is there a way to wait for the environment to be activated, perhaps a post-activation hook or something? I see that there's some system of "terminal providers" which I don't really understand, so is there some part of that API I can use? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 11 replies
-
We (and by that I mean @karrtikr 😁) are about to start looking into how we do terminal stuff to see if there's a better way to handle it. But I'm personally not aware of any hook from VS Code that can help in this situation based on how things are implemented. But do you need to have |
Beta Was this translation helpful? Give feedback.
-
You actually don't need to wait for the terminal to finish activating before sending your commands, you can send your command after we send ours, and VSCode ensures they're executed in sequence. There's currently a hack you can rely on based on an internal detail in our extension, this is where we send the activation commands: vscode-python/src/client/common/terminal/activator/base.ts Lines 30 to 46 in db8e1e2 We wait for |
Beta Was this translation helpful? Give feedback.
-
I have a related question that might belong in a new feature request, but I figured I'd start here in case it doesn't need a new issue. I have a python library that basically manages metadata around runs of machine learning tools. We are writing a VS Code extension for this library to provide a sort of UI for viewing the run information and starting new runs. The runs need the correct python environment activated. The easiest way to do this right now seems to be just to rely on the python plugin's existing environment configuration. This does seem to work fine when a python file is open (with the caveat that Austin described at the beginning about command ordering), but if a YAML file is open, then the terminal that we open lacks activation. My overarching goal: get a terminal that has the project's configured environment activated. It might be good enough to just have the path to the python executable, but from my old conda experience, true activation is better for catching any random environment variables that may actually be critically important. This is why I think #18978 (comment) is maybe not a complete solution here. Also, since our tool does a fair bit of subprocess stuff, I'm concerned that PATH modification (and thus activation) may be a necessary evil here. From looking at the vscode-python codebase, it seems like it would be ideal if I could access the vscode-python plugin's TerminalServiceFactory, essentially to be able to do this from my plugin: https://github.com/microsoft/vscode-python/blob/main/src/client/providers/terminalProvider.ts#L55-L57 Let me know if this belongs in a new issue, or if you need more info. |
Beta Was this translation helpful? Give feedback.
We (and by that I mean @karrtikr 😁) are about to start looking into how we do terminal stuff to see if there's a better way to handle it. But I'm personally not aware of any hook from VS Code that can help in this situation based on how things are implemented.
But do you need to have
PATH
manipulated? Could you use-m
with the selected Python interpreter to run the tool?