-
Notifications
You must be signed in to change notification settings - Fork 29
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
Improve Python environment activation #1791
Comments
How this currently worksThere are several options available to the user. We cater to the following combinations:
*** When the CLI is installed globally it could actually be a binary so cannot call with CoverageThis works for
ProblemsHowever, this approach falls down when there is an activation script available (e.g not The problem is not as simple as "just source the activation script" for the reasons stated above and because not all package managers have an activate script (e.g Proposed solutionI personally think the best thing to do right now would be to give the user the option to set their own additional environment variables for a DVC process (i.e anything that can require packages from a virtual environment). This does add to the setup overhead and will need to be documented BUT should only have to happen once per project per user. I think this would be a fair trade off and we may eventually be rescued by VS Code already let's users set these type of config variables for the integrated terminal: WDYT @shcheklein? |
@mattseddon qq - how do we get the MS Python's interpreter path? Could you share what API doc / place in out code? |
We use this API from the python extension: https://github.com/microsoft/vscode-python/blob/main/src/client/api.ts The part of the API that we use is this: const api: IExtensionApi = {
// 'ready' will propagate the exception, but we must log it here first.
ready: ready.catch((ex) => {
traceError('Failure during activation.', ex);
return Promise.reject(ex);
}),
...
settings: {
onDidChangeExecutionDetails: interpreterService.onDidChangeInterpreterConfiguration,
getExecutionDetails(resource?: Resource) {
const pythonPath = configurationService.getSettings(resource).pythonPath;
// If pythonPath equals an empty string, no interpreter is set.
return { execCommand: pythonPath === '' ? undefined : [pythonPath] };
},
}, |
@mattseddon thanks, have you found/checked by chance how does VS Code Terminal "understand" which script to run (how does communicate with MS Python)? |
It is the other way around. MS Python sends messages to the terminal with These parts of the VS Code API that they use: /**
* An individual terminal instance within the integrated terminal.
*/
export interface Terminal {
...
/**
* Send text to the terminal. The text is written to the stdin of the underlying pty process
* (shell) of the terminal.
*
* @param text The text to send.
* @param addNewLine Whether to add a new line to the text being sent, this is normally
* required to run a command in the terminal. The character(s) added are \n or \r\n
* depending on the platform. This defaults to `true`.
*/
sendText(text: string, addNewLine?: boolean): void;
...
/**
* An {@link Event} which fires when a terminal has been created, either through the
* {@link window.createTerminal createTerminal} API or commands.
*/
export const onDidOpenTerminal: Event<Terminal>;
/**
* Namespace for dealing with the current window of the editor. That is visible
* and active editors, as well as, UI elements to show messages, selections, and
* asking for user input.
*/
export namespace window {
...
/**
* An {@link Event} which fires when a terminal has been created, either through the
* {@link window.createTerminal createTerminal} API or commands.
*/
export const onDidOpenTerminal: Event<Terminal>;
... Finding anything in the |
This could potentially change as a result of microsoft/vscode-python#11039. |
Regarding getting more information from |
Thanks, Matt, I think I have a better picture of all this stuff in my head now.
It seems to be a reasonable approach. Nothing else besides just copy pasting the whole activation logic comes to my mind. Some additional thoughts / ideas that I had but that would require going too deep.
|
Hi! I was directed here by @shcheklein. I am running into a problem with the DVC extension and virtual envs. I have a codebase, and set up a Then, I am getting an error A potential culprit might be the pythonpath setting. In VS Code, I have an To sum up, I am using macOS and working with a conda environment. Any ideas about what might be the source of the issue and how to solve it? EDIT: I also tried to add the path to the Python interpreter into the
|
Another related issue that pops up while playing around with the extension is that once I try using the experiments tab (it does not work as mentioned above) to run an experiment, I can't run them anymore from the terminal using |
Does it depend on whether extension is active or not? Could you also please share the project layout and how do you run your script usually? I'm curios why do we need to do |
After I tried running the The project's structure is as follows:
And normally I run a script as |
Got it.
This is interesting. We'll check.
thanks, it makes sense. Why do you need to use |
Currently, I import the modules/scripts in the
|
@SoyGema could you please also add a summary of the road block that you hit with the env vars / PYTHONPATH? |
CONTEXT : Reproducing the experiments for workshop , with When executing
That leads to set PYTHONPATH in CLI with Then, pressing left button and executing
Which seems similar to the previous error , which might lead to "assume" or "hypothesize" that the Workspace UI path config doesn´t relate to defined PYTHONPATH in CLI CONFIG |
Looks like PYTHONPATH is now exposed by the Python Extension: https://github.com/microsoft/vscode-python/blob/main/src/client/api.ts#L80 |
This is however marked as deprecated so we need to wait for it to get moved into stable before we go ahead and use it: https://github.com/microsoft/vscode-python/blob/main/src/client/api.ts#L30 |
Currently for Python virtual environments we use
ms-python.python
's API to grab the currently selected Python interpreter and then we run:$python_bin -c "import sys; print(sys.executable)"
This gives us the absolute path of the interpreter. We then call
dvc
withpath/to/interpreter/python -m dvc with some args
. We also prepend the directory that the python interpreter is in to the system $PATH variable before running the command. This works for the basic case of DVC being installed within the virtual environment and is the suggested approach from an ongoing github discussion.Our current approach also works for global installs of DVC as long as the required dependencies are installed within the virtual environment. E.g for the demo project a global install of DVC does not work unless
dvclive
andruamel.yaml
are added to the virtual environment. I don't think there is a way around this for the brew installation. I looked into what can be done for a global install viapip
but I can recreate the same behaviour via the terminal after activating the virtual environment.I've been looking into how other extensions do this again and have found the following issues/discussions:
This seems to be an area that the
vscode-python
team is actively working on. We should keep an eye on those discussion tickets and see where they go. If they could expose the activation command in their API that would be ideal. Maybe we can get the correct PATH details from EnvironmentVariableCollection in the extension context.The text was updated successfully, but these errors were encountered: