Downloaded .py file not working in Jupyter - python

I'm new to programming and now taking the course MIT 6.0001. On problem set 2, they set a .py file, accompanied with a .txt file.
I ran .py file in Spyder with no difficulties (.txt file embedded just fine).
But when I run .py file in Jupyter, things won't run (as in screenshot).
I want to use Jupyter because it's much clearer than Spyder, and can give headers to cells to keep myself on track. Please help me to run things in Jupyter.
Here are link to the problem set (ZIP file)
File not running in Jupyter

Use
%load filename.py
to import the text of your python file into a cell in a Jupyter notebook and then save that notebook.
Jupyter isn't built to run/edit .py files, it's built to run/edit jupyter notebooks (ipython notebooks) (.ipynb)

Related

How can i stop vs code from making .ipynb files?

When i run example.ipynb file in vs code, it saves me a lot of files like:
example-caloifdf-498s-a54d-sdj8-caksjnbrhn83.ipynb
example-a60c6s44-9fax-4747-a9da-as9876fg34r8.ipynb
etc...
in the same folder where example.ipynb is in.
Questions:
What are those files?
How i can i stop vs code from making them\put them in another path if they are relevant?
Thank You.
That should actually be a bug. It's supposed to delete those files. There a consequence of us using jupyter under the covers to run your kernel.
This bug here should resolve it when we fix it:
https://github.com/microsoft/vscode-jupyter/issues/6510
.ipynb files aren't created by vs-code, they are jupyter notebook files generated when working with jupyter notebooks. They don't appear in anaconda or other IDEs which have built-in jupyter notebook support because there is no need to interact with the underlying .ipynb file directly.

How to open .ipynb file in Spyder?

without using iSpyder DOS shell commands, how can an .ipynb (Jupyter Notebook) be opened directly into Spyder on Windows? Even the online Jupyter Notebook site prompts for a relative directory path where the file is stored.
Why isn't there something that just loads the Notebook how it's supposed to look without typing a bunch of directory commands,
and why does Spyder's RUN button become greyed out when it loads the .ipynb file?
I have no idea what the .ipynb file format is compared to regular .py files
Opening lines of the .ipynb when loaded in Spyder are:
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
This does not look like python code whatsoever
You may check out https://github.com/spyder-ide/spyder-notebook
Once you install this, you can open native .ipynb files in spyder
From the website:
Spyder plugin to use Jupyter notebooks inside Spyder. Currently it
supports basic functionality such as creating new notebooks, opening
any notebook in your filesystem and saving notebooks at any location.
You can also use Spyder's file switcher to easily switch between
notebooks and open an IPython console connected to the kernel of a
notebook to inspect its variables in the Variable Explorer.
The jupyter notebook plugin currently works with Spyder 4.2.5 version and will not work with Spyder 5.X versions.
If you are using Spyder-5.x and therefore cannot use the Spyder-notebook plugin (see the other answers), an alternative is to export the .ipynb from Jupyter as an executable .py script first, then open the .py script in Spyder.
Jupyter->Files->Save and Export Notebook as->Executable script
The .py script produced preserves the ipython cells, using the
# In[<cell name or number>]:
syntax, for dividing the code into cells.
In Spyder, you can then right-click on the cell, and select Run cell to execute it in the Spyder console.
Beware, that all the cells (Code, Markdown, and Raw) are saved in the .py script. You may need to add '''block quotes''' around your Markdown and Raw cells, to prevent SyntaxErrors when running the .py script file.

Not able to get a normal jupyter notebook file

I'm starting a python project and when I'm opening a jupyter notebook in the directory I'm getting the following file. Normally there are other tabs such as kernel. Additionally I'm not able to run code using control + shift.
Jupyter notebooks are the files with extension .ipynb not with the extension .py which you are opening right now (main.py).
Look for a file with .ipynb extension and open it, it will run fine.

What is the difference between a .py file and .ipynb file?

I have read about .py and .ipy, also the difference between python, ipython and notebook.
But the question is: what is the real difference between .py and .ipynb file?
Is .ipynb file just more convenient to be run on jupyter notebook, or anything more? I am wondering because I am thinking about which format to be used for publishing on GitHub.
Thanks
.py is a regular python file. It's plain text and contains just your code.
.ipynb is a python notebook and it contains the notebook code, the execution results and other internal settings in a specific format. You can just run .ipynb on the jupyter environment.
Better way to understand the difference: open each file using a regular text editor like notepad (on Windows) or gedit (on Linux).
Save on git the .ipynb if you want to show the results of your script for didatic purposes, for example. But if you are going to run your code on a server, just save the .py
Adding #Josir answer, the below information is very useful for open .ipynb file using PyCharm.
Create a new Python project in Pycharm
Specify a virtual environment, and install the jupyter package(pip install jupyterlab).
Run the server using the jupyter-lab command.
Browser will open the jupyter notebook like below, there you can execute the .ipynp file.
Here is documentation https://jupyterlab.readthedocs.io/en/latest/
py means PYthon
ipynb means Interactive PYthon NoteBook - which is now known as Jupyter notebook.
The latter one is merely a Python script with descriptive contents - you describe what your data is doing by means of Python script and some funny texts. That's pretty much it - and also, you need a specific editor e.g. PyCharm or Google Collab to open and run it.
I think the answer here might help you: https://stackoverflow.com/a/32029027/11924650
.ipy indicates that it's an IPython script. The only difference between IPython scripts and normal Python scripts is that IPython scripts can use IPython magics, e.g. %timeit, and run system commands as !echo Hi.

How to run .py codes on jupyter lab?

How can I run .py files from jupyter lab? I have spent my all coding life using jupyter notebook and jupyter lab but replication codes of research papers are mostly in .py file format
For instance, this is a github repository for beta variational autoencoder. As you can see from the repository, these kinds of repositories are usually comprised of main.py, model.py, which looks a lot different from .ipynb format that I usually use.
Can someone share how to comfortably run these kinds of .py codeson jupyter lab? I would appreciate it a lot if someone tells me a video or an article explaining how to run these .py codes on jupyter lab comfortably.
"...how to run these .py codes on jupyter lab comfortably."
Basically the Jupyter's IPython interface allows you to do magic commands that commands within a shell.
Here is the magic for python subprocess.
You can use:
%python -m /path/to/myfile
Then execute the cell and the command will run in the cell and the output cell is the standard output for this run.
You can also make this run in tmux or other tools to make it as a managed background job.
There is a rather thorough discussion of how to interact with .py files from Jupyter notebook here:
How to load/edit/run/save text files (.py) into an IPython notebook cell?
(No terminal window is needed.)
Find File-> new launcher -> other -> terminal, then you use command line run your python file, like "python xxx.py"

Categories