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

Respect sys.flags.safe_path #1311

Closed
siddharthab opened this issue Dec 18, 2024 · 3 comments
Closed

Respect sys.flags.safe_path #1311

siddharthab opened this issue Dec 18, 2024 · 3 comments

Comments

@siddharthab
Copy link

In the following line, the script directory path is added even if Python was run with sys.flags.safe_path (most often by setting the env var PYTHONSAFEPATH=1).

sys.path.insert(0, str(here))

Not having the ability to set safe path causes problems when there are sibling Python files with names that clash with installed Python modules.

Thanks to @DonJayamanne for helping find the root cause in microsoft/vscode-jupyter#16235.

@Carreau
Copy link
Member

Carreau commented Feb 12, 2025

I doubt this is the location that adds the problematic sys.path entry as this should be used only when building ipykernel wheels.

@Carreau
Copy link
Member

Carreau commented Feb 12, 2025

$ python -m ipykernel --help-all | grep cwd  -A 5
--ignore-cwd
    Exclude the current working directory from sys.path
    Equivalent to: [--InteractiveShellApp.ignore_cwd=True]
--no-ignore-cwd
    Include the current working directory in sys.path
    Equivalent to: [--InteractiveShellApp.ignore_cwd=False]
...
--InteractiveShellApp.ignore_cwd=<Bool>
    If True, IPython will not add the current working directory to sys.path.
    When False, the current working directory is added to sys.path, allowing
    imports of modules defined in the current directory.
    Default: False
..
--IPKernelApp.ignore_cwd=<Bool>
    If True, IPython will not add the current working directory to sys.path.
    When False, the current working directory is added to sys.path, allowing
    imports of modules defined in the current directory.
    Default: False

Updating the kernel launch config file to pass this flag (or set a global configuration file with the proper configuration works:

{
  "argv": [
    "$python_interpreter",
    "-Xfrozen_modules=off",
    "-m",
    "ipykernel_launcher",
    "-f",
    "{connection_file}",
    "--ignore-cwd"
  ],
  "display_name": "global",
  "language": "python",
  "metadata": {
    "debugger": true
  }
}

or config file with:

c.InteractiveShellApp.ignore_cwd=False

@Carreau Carreau closed this as completed Feb 12, 2025
@siddharthab
Copy link
Author

Thank you!

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

No branches or pull requests

2 participants