Repository navigation
Run Python formatters/linters/... in an environment #14794
Description
Current situation
I have a specific setup for defining my development environment (I use Nix).
What I can do for now is create an environment, which is supposed to have Python, black, flake8, ...
This environment can be built and oaded to the shell using the nix-shell command in the terminal for instance.
I can run python -m black in this shell without issue.
Once the Python environment is built with Nix, it is defined statically, which means that I do have an executable /nix/store/somehash/bin/python that corresponds to my environment (it is actually a wrapper in bash that sets the right environment variables before starting Python).
When I use this Python wrapper, the VS Code Python extension should pickup the same environment and have no issue running for instance the formatter through python -m black for instance.
Feature I would like
I would like the Python extension to either:
- properly run such a wrapped Python executable bash in order to pickup the environment variables
or - allow the Python extension to run its commands in a shell, for instance the one specified by
terminal.integrated.automationShell
Investigation
I did my own investigation, and found out that
the formatter is run using this:
| .exec(executionInfo, { cwd, throwOnStdErr: false, token }, document.uri) |
| return pythonExecutionService.execModule(executionInfo.moduleName!, executionInfo.args, options); |
| const result = await this.deps.exec(executable.command, executable.args, opts); |
| public execObservable(file: string, args: string[], options: SpawnOptions = {}): ObservableExecutionResult<string> { |
So I guess you spawn the process. Don't know why it doesn't pickup the environment variables the bash Python wrapper defines.
Activity
karrtikr commentedon Nov 25, 2020
Thanks for the report! We just wanted to quickly acknowledge we received it and we will triage this as soon as we can.
GuillaumeDesforges commentedon Dec 16, 2020
I just noticed that the Python extension is able to pick up on the PATH made by Python venv. Would it be possible to allow to customize the hook (e.g. in my case use
nix-shell shell.nixinstead ofsource .venv/bin/activate) ? This would also allow to support other tools that provide shells such as Poetry.karrtikr commentedon Dec 16, 2020
Apologies for the late response, the notification got skipped. #8870 should help you out to customize the hook. FWIW we also have #8372 to add poetry support explicitly. Please upvote these issues to bump the priority, closing in favor of that.