SoFunction
Updated on 2024-12-19

Problems saving python code in .py format using jupyter notebook

jupyter notebook saves python code in .py format

Jupyter notebook is derived from the IPython project started by Fernando Perez. IPython is an interactive shell that is similar to a regular Python shell, but has some nice features (such as syntax highlighting and code completion).

Type in jupyter notebook:

%%writefile 
train_v = 10
def train_add(list_n):
    train_sum = 0
    for i in range(len(list_n)):
        train_sum += list_n[i]
    return train_sum
list_n = [2,3,4,5,6]
print(train_add(list_n))

%%writefile Indicates the file to save the file as, which is displayed after running:

Writing

%run 
20

Run %run to run a python file.

import os
('.')  #Display current road power
'F:\\'
('')  #Remove files

If you need to delete it, use remove to do so.

jupyter notebook code completion extension installation encounters Jupyter command `jupyter-contrib` not found.

locomotive

As we all know, Jupyer notebook is small in size, especially suitable for beginners' code debugging. Each Cell can save the history of variables. However, its relative pycharm beauty is that it does not come with the "code completion" function, fortunately, there are extensions to improve.

I bought a new computer and I also wanted to install that extension - jupyter_contrib_nbextension - the way it was installed on my old computer, but I couldn't get it to install following the old tutorials, and then looked for a solution, tried several useless threads, felt heartbroken and misguided, and finally found a solution to use as a note.

In fact, the solution of the post is not my solution, so I would like to thank the blogger "duqiumail" about "Jupyter notebook install extensions (nbextensions) prompt "Jupyter command jupyter-contrib not found." The link is at the end of the original link.

I am using the python version Anaconda3-2019.10, python37

Older tutorials for installing plugins (can be skipped)

windows + R, typecmd Enter the command line interface

pip install jupyter_contrib_nbextensions -i /simple
jupyter contrib nbextension install --user 
# Here the old error, Jupyter command `jupyter-contrib` not found, was reported, but it was not reported on the old computer.
pip install --user jupyter_nbextensions_configurator 
jupyter nbextensions_configurator enable --user

Then when you go to jupyter notebook, you'll see that the menu Wolf has been added.Nbextensionsoption, and then check theHinterland Check the box. Great job.

However, in the case ofjupyter contrib nbextension install --user The place where the old error was reported.

Jupyter command jupyter-contrib not found.

Solution - New Installation

1. First use the following command

Uninstall older versions of jupyter_contrib_nbextensions and upyter_nbextensions_configurator: Uninstall the previous installations with the cmd commands respectively

pip uninstall jupyter_contrib_nbextensions
pip uninstall jupyter_nbextensions_configurator

2. Re-download and install:

2.1 Installation by cmd command

pip install -i /simple --trusted-host  jupyter_contrib_nbextensions

2.2 Then cmd into~Lib\site-packages\jupyter_contrib_nbextensions catalogs

for example

C:\ProgramData\Anaconda3\Lib\site-packages\jupyter_contrib_nbextensions

Sometimes it will be in the user folder, such as

C:\Users\he\Python\Python38\Lib\site-packages\jupyter_contrib_nbextensions

2.3 In this directory, cmd execute the following command to install manually

python  install

2.4 Then install

jupyter contrib-nbextension install --user

The problem of "Jupyter command jupyter-contrib not found.

3 Next, continue with the installation

pip install -i /simple --trusted-host  jupyter_nbextensions_configurator
jupyter nbextensions_configurator enable --user

When you restart jupyter notebook, you'll see the tool Nbextensions in the toolbar.

summarize

The above is a personal experience, I hope it can give you a reference, and I hope you can support me more.