Skip to content
Advertisement

Jupyter notebook cell code not executing in VSCode when there is a file named random.py in the folder

I have just started using Jupyter notebooks in VSCode and have come across a strange issue. I made a folder for keeping my lab programs and there is a file named random.py in the folder. When I try to execute a code cell of my Jupyter notebook which is present in the same folder then I get an error ‘Kernel died with exit code 1’. If I delete the contents of the random.py file and just make it an empty file then also the issue persists. The issue is resolved if I remove the file named random.py or change it’s name to something else like random1.py.

I have read about ‘Kernel died with exit code 1’ error here and here.

My Environment data

  • VS Code version: 1.59.0
  • Jupyter Extension version: v2021.8.1236758218
  • Python Extension version: v2021.8.1105858891
  • OS: Windows 10
  • Python version: 3.8.5 64-bit
  • Type of virtual environment used: Global (I am not completely sure but this is the environment based on the vscode docs that I’ve read on python environments) Here is the image to make this clear – image showing that I don’t have any ‘base: conda’ or something like that
  • Jupyter server running: Local

I do not have Anaconda installed on my system. I tried the fixes told in the above links but none of them worked. If I put the Jupyter notebook in any other folder and run the cell code then it runs perfectly fine. I want to know that is it possible for a Jupyter notebook to not run when there’s a file named ‘random.py’ present in the same folder? I have not seen this issue reported anywhere else so I am putting it here so that I can get some help on it.

Here are the screenshots for the proof –

  1. not working when random.py file is present
  2. working when the filename is changed

Here is the complete error message –

Kernel died with exit code 1. Traceback (most recent call last): File “C:UsersHPAppDataLocalProgramsPythonPython38librunpy.py”, line 194, in _run_module_as_main return run_code(code, main_globals, None, File “C:UsersHPAppDataLocalProgramsPythonPython38librunpy.py”, line 87, in run_code exec(code, run_globals) File “C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesipykernel_launcher.py”, line 15, in from ipykernel import kernelapp as app File “C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesipykernel_init.py”, line 2, in from .connect import * File “C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesipykernelconnect.py”, line 12, in import jupyter_client File “C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesjupyter_client_init.py”, line 4, in from .connect import * File “C:UsersHPAppDataLocalProgramsPythonPython38libsite-packagesjupyter_clientconnect.py”, line 16, in import tempfile File “C:UsersHPAppDataLocalProgramsPythonPython38libtempfile.py”, line 45, in from random import Random as _Random ImportError: cannot import name ‘Random’ from ‘random’ (d:LABSAI Lablab3random.py)

Advertisement

Answer

My guess is that you’re interfering with python’s built in ‘random’ module and something is accidentally importing your file instead of the proper module. That is why the solution is changing the name of your file.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement