Skip to content

Remove idlelib from sys.path in IDLE run process #134300

Open
@Locked-chess-official

Description

@Locked-chess-official

When use IDLE to open a file and start a new IDLE windows from "Python Shell" button, you can import the modules that are the child modules in the module "idlelib", such as "help", "pyshell". Think for that there are many schools still use IDLE to teach python to prevent the students to install malicious plugins from other IDE, if the students try to import these modules, it will result in misleading.

import help

This code is wrong in cmd and any other IDE but "normal" in IDLE, and what is imported is "idlelib.help".

Activity

gaogaotiantian

gaogaotiantian commented on May 20, 2025

@gaogaotiantian
Member

Hi, there's a quick suggestion - the title of the issue should be more descriptive about the topic, rather than a very generic question, so the triagers and core devs can understand what's the issue about (whether it's in their area of expertise) without clicking into it.

picnixz

picnixz commented on May 20, 2025

@picnixz
Member

cc @terryjreedy I don't understand the issue but since it seems to be IDLE related, I'm pinging you to have a look and decide the scope.

ronaldoussoren

ronaldoussoren commented on May 20, 2025

@ronaldoussoren
Contributor

Which version of Python are you using? And what OS are you on?

On macOS I get:

Python 3.12.10 (v3.12.10:0cc81280367, Apr  8 2025, 08:46:59) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Enter "help" below or click "Help" above for more information.
import help
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import help
ModuleNotFoundError: No module named 'help'
terryjreedy

terryjreedy commented on May 20, 2025

@terryjreedy
Member

This is a subissue of #69674; I am leaving this open to patch this specific issue, which is that .../idlelib is sometimes on sys.path in the separate user process where user code is normally executed. ('Normally', means when option -n is not added to IDLE startup.) I noted in 2015 that this depends on how IDLE is started; and added today that it may have changed with version. It also depends the system. With 3.14 on my Win 10, >>> import help works. On my macbook with Catalina, it raises as Ronald shows above. (Note to anyone else: the splash line Enter "help" below ... is IDLE Shell specific.) Checking sys.path (most easily with for p in sys.path: print(p) shows that .../idlelib is present on Windows but not on Mac.

@Locked-chess-official What OS, python version, and IDLE startup method did you use.

I think adding the following to idlelib.run near the top should work.

for i in (0, 1):
   if sys.path[i].endswith('idlelib'):
    del sys.path[i]
    break
changed the title [-]Is it needs to be fixed or writed in docs for warning?[/-] [+]Remove idlelib from sys.path in IDLE run process[/+] on May 20, 2025
Locked-chess-official

Locked-chess-official commented on May 21, 2025

@Locked-chess-official
Author

What OS, python version, and IDLE startup method did you use.

Windows, 3.12 and 3.13

Locked-chess-official

Locked-chess-official commented on May 21, 2025

@Locked-chess-official
Author

Windows, 3.12 and 3.13  Windows, 3.12 和 3.13
When use idle.bat to start, it sames too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Remove idlelib from sys.path in IDLE run process · Issue #134300 · python/cpython

      Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

      Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant