jupyter / jupyter_console

Jupyter Terminal Console
http://jupyter-console.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
247 stars 146 forks source link

System imports can be overriden by user-space files such as `queue.py` #297

Open VorontsovIE opened 8 months ago

VorontsovIE commented 8 months ago

Sometimes I run jupyter notebooks from Downloads folder (or from Telegram Downloads). Typically in this folder there are dozens of python files from my students and some are named smth like queue.py.

When I open a notepad located in a folder having such files, import queue under the hood of jupyter loads these files instead of system library queue. And of course, everything crashes. I added a log-trace below.

Indeed I know how to deal with such a problem, usually I just rename my queue.py into smth different so that python can choose stdlib version. But it can be embarassing for people who see it for the first time.

Probably it's possible to use importlib machinery not to override system imports with user-space python files?

[I 2023-10-24 13:54:40.584 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (1/5), new random ports
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/ilya/.local/lib/python3.11/site-packages/ipykernel_launcher.py", line 15, in <module>
    from ipykernel import kernelapp as app
  File "/home/ilya/.local/lib/python3.11/site-packages/ipykernel/__init__.py", line 5, in <module>
    from .connect import *  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilya/.local/lib/python3.11/site-packages/ipykernel/connect.py", line 11, in <module>
    import jupyter_client
  File "/home/ilya/.local/lib/python3.11/site-packages/jupyter_client/__init__.py", line 3, in <module>
    from .asynchronous import AsyncKernelClient
  File "/home/ilya/.local/lib/python3.11/site-packages/jupyter_client/asynchronous/__init__.py", line 1, in <module>
    from .client import AsyncKernelClient  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/ilya/.local/lib/python3.11/site-packages/jupyter_client/asynchronous/client.py", line 8, in <module>
    from ..channels import AsyncZMQSocketChannel, HBChannel
  File "/home/ilya/.local/lib/python3.11/site-packages/jupyter_client/channels.py", line 8, in <module>
    from queue import Empty
ImportError: cannot import name 'Empty' from 'queue' (/home/ilya/Downloads/Telegram Desktop/queue.py)

Current version:

{'commit_hash': 'a2af83ddf',
 'commit_source': 'installation',
 'default_encoding': 'utf-8',
 'ipython_path': '/home/ilya/.local/lib/python3.11/site-packages/IPython',
 'ipython_version': '8.16.1',
 'os_name': 'posix',
 'platform': 'Linux-4.13.16-041316-generic-x86_64-with-glibc2.27',
 'sys_executable': '/usr/local/bin/python3',
 'sys_platform': 'linux',
 'sys_version': '3.11.3 (main, Apr  7 2023, 12:53:30) [GCC 7.5.0]'}