Ask Your Question
2

Creating a file for user defined functions using JupyterLab environment.

asked 2020-11-03 00:49:22 +0100

Oshio gravatar image

updated 2021-03-12 20:55:16 +0100

FrédéricC gravatar image

I want to create a function in a separate file, and use that function in another one. Here is the example:

#my_file.ipynb

def my_function(n):
    return n*2

#main.ipnyb

attach('my_file.ipynb')
my_function(2)

But when doing this at the JupyterLab I receive the following erros:

(1) ValueError: unknown file extension '.ipynb' for load or attach (supported extensions: .py, .pyx, .sage, .spyx, .f, .f90, .m)

(2) NameError: name 'my_function' is not defined

This method seems to work for .sage files, but is not working for '.ipynb'.

There is some way to accomplish the result demonstrated through my example using only JupyterLab environment?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2020-11-04 14:50:44 +0100

Sébastien gravatar image

updated 2020-11-04 14:52:16 +0100

See the following page from the documentation of Jupyter notebook for Importing Jupyter Notebooks as Modules.

You may also consider just writing the code you want to import in a plain .py or .sage file in the same directory which allows you easily to import, run, load or attach to a jupyter notebook.

edit flag offensive delete link more

Comments

This may need adaptation to Sage (think : preparser...).

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 2020-11-05 11:07:57 +0100 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-11-03 00:49:22 +0100

Seen: 610 times

Last updated: Nov 04 '20