0

I wanted to ask how to run in Jupyter Notebook the %run command. So say I have another notebook I want to run in parall called notebook.ipynb. However, I want to call this command to the notebook name from string. So I want something like this:

name = 'notebook.ipynb'
exec("%run "+name)

This results in an error.

How can I overcome this? Thanks

4
  • what error? exec executes python script and that doesn't seem like one so I don't know much about Jupyter but You may have to use os.system(f'%run {name}') (first import os) this runs it from cmd on windows tho don't know about other os Commented Apr 13, 2021 at 12:22
  • 1
    %run is not Python, it's a Jupyter thing, so it makes no sense to the interpreter if you try to exec it. Look instead at the way to do what you want in Python. It's called the subprocess module. Commented Apr 13, 2021 at 12:25
  • I get the following error: Traceback (most recent call last): File "C:\Users\...\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3319, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "<ipython-input-7-13fcb148f6bc>", line 1, in <module> exec('%run construct_mono.ipynb') File "<string>", line 1 %run construct_mono.ipynb ^ SyntaxError: invalid syntax Commented Apr 13, 2021 at 12:36
  • I tried the following using subprocess: import subprocess subprocess.run("construct_mono.ipynb") but it gives me an error as well. I just want to run the entire notebook in parallel. Commented Apr 13, 2021 at 12:37

1 Answer 1

3

So in the end it wasn't submodule that I was after.

I just needed to use $ to evaluate Python variable for Jupyter magic function.

notebook_name = 'construct_Test.ipynb'

%run $notebook_name

Thanks

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.