Error when I save code to Jupiter Notebook file

Asked 2 years ago, Updated 2 years ago, 117 views

"I am studying Python by referring to ""Everyone's Python"", but even if I type in the code below, it doesn't work as written in the reference book."What's wrong?
(I use juypter notebook)

#!/usr/bin/env python 

class StrDict(dict):
    def__init__(self):
        pass
    def__setitem__(self, key, value):
        if not isinstsnce(key,str):
            raise ValueError("Key must be stream unicode.")
        dict.__setitem__(self, key.value)

Save this to strdict.py and jupyter notebook files and

 from strdict import StrDict

I typed it in the next cell.

NameError Traceback (most recent call last)
<ipython-input-1-d320d56b1f78>in<module>()
---->1 from strdict import StrDict

~\strdict.py in<module>()
      3   {
      4"cell_type":"code",
---->5 "execution_count": null,
      6 "metadata": {},
      7 "outputs": [ ],

NameError: name 'null' is not defined

That's what happened.What should I do?

python jupyter-notebook

2022-09-29 21:45

2 Answers

Save this to strdict.py and jupyter notebook files and

It's different here.
You can create files in ipynb format on the Jupiter Notebook.
To create strdict.py, try using a text editor other than Jupiter Notebook.

To put it in more detail, the contents of the save format ipynb on the Jupiter Notebook are in json format.When I try to read this in Python import statement, Python tries to interpret the json format as Python grammar. null does not exist in Python grammar or built-in variables, so I get the error NameError: name 'null' is not defined.


2022-09-29 21:45

How to create a text file in Jupiter

Jupiter also seems to have a way to create and edit text files that are not in Notebook (ipynb format).This may be a supplement to the Already available answers.

When creating a new non-Notebook file, select Text File from Other: instead of Notebook:.

New File Creation Menu

Create a text file as above, or click on an existing text file to go to the edit screen.The screen configuration is different from when I opened the Notebook, so I think I can distinguish it.

text file editing screen

(The instructions and screenshots are from Jupiter 4.4.0)


2022-09-29 21:45

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.