-
Notifications
You must be signed in to change notification settings - Fork 295
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
python.envFile is not respected by Python code executed in Notebook and IW #9691
Comments
Thanks for the issue. I'm not sure if we should be using another extension's setting directly like that. Perhaps we can introduce our own env file setting that would default to Python's? We will discuss |
We could use |
This seems to also apply to def _reparent_env_parts():
import os, sys
def _pluck_prefixed(file_pattern: str, prefix: str) -> str:
file_path = file_pattern.replace("${workspaceFolder}", WORKSPACE_FOLDER)
with open(file_path, 'r') as f:
for l in f.readlines():
line = l.strip()
if line.startswith(prefix):
return line[len(prefix):]
WORKSPACE_FOLDER = os.path.dirname(os.environ['VIRTUAL_ENV'])
env_pattern = _pluck_prefixed('${workspaceFolder}/.vscode/settings.json', '"python.envFile": ')
env_pattern = env_pattern.strip()
env_pattern = env_pattern[1:len(env_pattern) - 2]
pypath = _pluck_prefixed(env_pattern, 'PYTHONPATH=')
python_path = [
os.path.join(WORKSPACE_FOLDER, p)
for p in pypath.split(':')
]
os.environ['PYTHONPATH'] = ':'.join(python_path)
sys.path[1:1] = python_path
_reparent_env_parts()
del _reparent_env_parts // User settings.json
"jupyter.runStartupCommands": [
"def _reparent_env_parts():",
" import os, sys",
" def _pluck_prefixed(file_pattern: str, prefix: str) -> str:",
" file_path = file_pattern.replace('$' + '{workspaceFolder}', WORKSPACE_FOLDER)",
" with open(file_path, 'r') as f:",
" for l in f.readlines():",
" line = l.strip()",
" if line.startswith(prefix):",
" return line[len(prefix):]",
" WORKSPACE_FOLDER = os.path.dirname(os.environ['VIRTUAL_ENV'])",
" env_pattern = _pluck_prefixed('${workspaceFolder}/.vscode/settings.json', '\"python.envFile\": ')",
" env_pattern = env_pattern.strip()",
" env_pattern = env_pattern[1:len(env_pattern) - 2]",
" pypath = _pluck_prefixed(env_pattern, 'PYTHONPATH=')",
" python_path = [",
" os.path.join(WORKSPACE_FOLDER, p)",
" for p in pypath.split(':')",
" ]",
" os.environ['PYTHONPATH'] = ':'.join(python_path)",
" sys.path[1:1] = python_path",
"_reparent_env_parts()",
"del _reparent_env_parts",
], |
I beleive the solution was to respsect the .env file setting form Python when dealing with Python kernels & not others. |
Solution
|
If jupyter and python only respect .env file, could we make the PYTHONPATH in # .env
if windows:
PYTHONPATH = "D:/path1;D:/path2;D:/path3"
elif linux:
PYTHONPATH = "~/lib/path1:~/lib/path2:~/lib/path3" |
Applies To
What happened?
Environment variables defined in an
.env
file in the workspace root are available in notebooks, but it is surprising that only root.env
files are supported, rather than using thepython.envFile
setting like other VSCode Python functionality.The
.env
file path appears to be hardcoded here.VS Code Version
1.66.2
Jupyter Extension Version
v2022.3.1000901801
Jupyter logs
No response
Coding Language and Runtime Version
No response
Language Extension Version (if applicable)
No response
Anaconda Version (if applicable)
No response
Running Jupyter locally or remotely?
No response
The text was updated successfully, but these errors were encountered: