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

Fixes related to loading of env variables #10843

Merged
merged 6 commits into from
Jul 20, 2022
Merged

Fixes related to loading of env variables #10843

merged 6 commits into from
Jul 20, 2022

Conversation

DonJayamanne
Copy link
Contributor

Part of #10359


// If user asks us to, set PYTHONNOUSERSITE
// For more details see here https://github.com/microsoft/vscode-jupyter/issues/8553#issuecomment-997144591
// https://docs.python.org/3/library/site.html#site.ENABLE_USER_SITE
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicate code can be replaced with KernelEnvironmentVariablesService.getEnvironmentVariables

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now both raw kernels and jupyter kernels will use the same set of code to generate env variables for staring of kernels.

traceInfo(
`No custom variables for Kernel as interpreter path is not defined for kernel ${kernelSpec.display_name}`
);
return kernelEnv;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will always return the env variables in this method, never undefined
i.e. merge process.env with kernelspec.env and the like.

// Also applies to `!java` where java could be an executable in the conda bin directory.
if (interpreter) {
const env = kernelEnv || process.env;
this.envVarsService.prependPath(env, path.dirname(interpreter.uri.fsPath));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't necessary as we already do this further below (exact same code)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But below doesn't return early? It doesn't look the same to me?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this part of the bug?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats correct, however if you check the code below its the same, as kernelEnv and customEnvVars will be empty, hence most of the code below will be noops, but at least this way its consistent, else we have a lot of branching if conditions, which just complicates it.

}
otherEnvPathKey = Object.keys(kernelEnv).find((k) => k.toLowerCase() == 'path');
if (otherEnvPathKey && kernelEnv[otherEnvPathKey]) {
this.envVarsService.appendPath(mergedVars, kernelEnv[otherEnvPathKey]!);
}
if (customEditVars.PYTHONPATH) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed to make is clear that these are custom Environment variables. I am guessing the Edit was a typo from my end.

@codecov-commenter
Copy link

codecov-commenter commented Jul 18, 2022

Codecov Report

Merging #10843 (8f24f02) into main (599ade0) will increase coverage by 0%.
The diff coverage is 68%.

@@          Coverage Diff           @@
##            main   #10843   +/-   ##
======================================
  Coverage     63%      63%           
======================================
  Files        482      482           
  Lines      33652    33600   -52     
  Branches    5488     5471   -17     
======================================
- Hits       21278    21276    -2     
+ Misses     10320    10294   -26     
+ Partials    2054     2030   -24     
Impacted Files Coverage Δ
...ommon/process/environmentActivationService.node.ts 72% <ø> (+2%) ⬆️
src/platform/common/variables/types.ts 100% <ø> (ø)
src/kernels/jupyter/jupyterKernelService.node.ts 74% <46%> (-8%) ⬇️
.../kernels/raw/launcher/kernelEnvVarsService.node.ts 81% <100%> (+12%) ⬆️
src/kernels/raw/launcher/kernelProcess.node.ts 70% <0%> (+<1%) ⬆️
src/kernels/variables/debuggerVariables.ts 68% <0%> (+<1%) ⬆️
...lPythonAndRelatedNonPythonKernelSpecFinder.node.ts 71% <0%> (+<1%) ⬆️
src/platform/common/process/baseDaemon.node.ts 51% <0%> (+1%) ⬆️
src/standalone/api/kernelApi.ts 73% <0%> (+1%) ⬆️
src/platform/common/process/proc.node.ts 82% <0%> (+1%) ⬆️
... and 8 more

@@ -276,75 +274,35 @@ export class JupyterKernelService implements IJupyterKernelService {
);
specModel.argv[0] = interpreter.uri.fsPath;
}
// Get the activated environment variables (as a work around for `conda run` and similar).
// This ensures the code runs within the context of an activated environment.
const interpreterEnv = await this.activationHelper
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed duplicate code in favor of kernelEnvVars.getEnvironmentVariables
Now both raw and jupyter kernels (both) use the exact same code to generate env variables for kernels.

@DonJayamanne DonJayamanne marked this pull request as ready for review July 18, 2022 18:56
@DonJayamanne DonJayamanne requested a review from a team as a code owner July 18, 2022 18:56
@jtele2
Copy link

jtele2 commented Sep 27, 2022

I still cannot get this to work. How do I set environment variables from a .env file for use within Python Interactive (Jupyter) using #%% cell distinguishers with .py files? Does this fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants