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

python.envFile is not respected by Python code executed in Notebook and IW #9691

Closed
1 of 2 tasks
dmartin opened this issue Apr 15, 2022 · 6 comments · Fixed by #10876
Closed
1 of 2 tasks

python.envFile is not respected by Python code executed in Notebook and IW #9691

dmartin opened this issue Apr 15, 2022 · 6 comments · Fixed by #10876
Labels
feature-request Request for new features or functionality notebook-execution Kernels issues (start/restart/switch/execution, install ipykernel)

Comments

@dmartin
Copy link

dmartin commented Apr 15, 2022

Applies To

  • Notebooks (.ipynb files)
  • Interactive Window and/or Cell Scripts (.py files with #%% markers)

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 the python.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

@dmartin dmartin added bug Issue identified by VS Code Team member as probable bug needs-triage labels Apr 15, 2022
@amunger
Copy link
Contributor

amunger commented Apr 15, 2022

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

@rchiodo rchiodo added the notebook-execution Kernels issues (start/restart/switch/execution, install ipykernel) label Apr 18, 2022
@rchiodo
Copy link
Contributor

rchiodo commented Apr 18, 2022

We could use python.envFile for just python kernels.

@rchiodo rchiodo added enhancement and removed bug Issue identified by VS Code Team member as probable bug labels Apr 18, 2022
@micimize
Copy link

micimize commented May 2, 2022

This seems to also apply to *.py scripts. I'm still a bit unsure if it was ignoring my env entirely or just setting it relative to the script location, but I came up with a runStartupCommands solution that seems to work regardless:

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",
],

@greazer greazer added feature-request Request for new features or functionality and removed enhancement labels May 4, 2022
@DonJayamanne
Copy link
Contributor

I beleive the solution was to respsect the .env file setting form Python when dealing with Python kernels & not others.

@DonJayamanne DonJayamanne changed the title python.envFile is not respected python.envFile is not respected by Python code executed in Notebook and IW Jul 18, 2022
@DonJayamanne
Copy link
Contributor

Solution

  • Respsect the .env file setting form Python only when dealing with Python kernels
  • When running regular python code, such as launching Jupyter, etc, we'll not load from the pyhton.env file setting.

@liubenyuan
Copy link

If jupyter and python only respect .env file, could we make the PYTHONPATH in .env compatible (os.pathsep) for both linux and windows operating systems?

# .env
if windows:
    PYTHONPATH = "D:/path1;D:/path2;D:/path3"
elif linux:
    PYTHONPATH = "~/lib/path1:~/lib/path2:~/lib/path3"

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request for new features or functionality notebook-execution Kernels issues (start/restart/switch/execution, install ipykernel)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants