File not found when running a file in JupiterLab console

1.6k views Asked by At

Every time when I try to run a file in the JupiterLab console I get the following message:

ERROR:root:File 'thing.py' not found.

In this case, my file is called thing.py and I try to run it with the trivial run thing.py command in the console. The code is running and it gives me correct results when executed in the console, but I wanted to have it saved, so I put it in a JupiterLab text file and changed the extension to .py instead of .txt. But I get the aforementioned message regardless of which file I try to run. I am new to JupiterLab and admit that I might have missed something important. Every help is much appreciated.

2

There are 2 answers

11
Gray On

You might want to understand exactly what a Jupyter Lab file is, and what a Jupyter Lab file is not. The Jupyter Notebooks have the extension, .ipynb.

So anyway, the Jupyter Notebooks are not saved or formatted with python extensions. There are no Jupyter Notebooks or Jupyter Labs ending with the .py extension. That means Jupyter will not recognize an extension having .py, or .txt or .R or etc.... Jupyter opens, reads, and saves files having the .ipynb extension.

Jupyter Notebooks are an open document format based on JSON.

Jupyter can export in a number of different formats. Under the File tab, is the Export feature. The last time I looked there were about 20 different export formats. But there isn't a python or .py export format. A Jupyter file can also be Downloaded. Also under the File tab is the Download feature. This will download a standard text formatted JSON file. JSON files are mostly unreadable unless you've spent years coding JSON.

So there's not much purpose in downloading the Jupyter file unless you are working on a remote server and cannot save your work at that site. And it makes much more sense to save and copy the Jupyter file in its native Jupyter format - that means having the extension, .ipynb . Then just open and use that file on another PC.

Hopefully this should clarify why Jupyter won't open any .py or .txt files.

0
Paul H On

If you're running Jupyterlab you should be able:

  1. to create a new file & paste in your commands

enter image description here

  1. Rename that file to "thing.py"

enter image description here

  1. And then open a console in the same Jupyterlab instance and run that file. Notice that you can see "thing.py" in the file explorer on the left:

enter image description here

  1. Alternatively, you can use the %load magic command in a notebook to dynamically load the code into a notebook's cell.

enter image description here