How to obtain Jupyter Notebook's path?

前端 未结 4 2148
礼貌的吻别
礼貌的吻别 2021-01-04 21:20

Is there a function to obtain a Notebook\'s path?

I\'ve Googled a little on the subject but didn\'t find a simple way to do it... I want to obtain the Notebook\'s p

相关标签:
4条回答
  • 2021-01-04 21:41

    use this in cell

    %%javascript
    IPython.notebook.kernel.execute('nb_name = "' + IPython.notebook.notebook_name + '"')
    print(nb_name)
    
    0 讨论(0)
  • 2021-01-04 21:49

    You can just use "pwd" which stands for print working directory. enter image description here

    0 讨论(0)
  • 2021-01-04 21:50

    if you can open it you can use this function

    1-open your Jupyter notebook 2- write this function 3-it will print out the path

    pwd
    

    if not navigate to your python installation folder open folder scripts and there you will find it.

    hope this may help others

    0 讨论(0)
  • 2021-01-04 21:59

    TLDR: You can't

    It is not possible to consistently get the path of a Jupyter notebook. See ipython issue #10123 for more information. I'll quote Carreau:

    Here are some reasons why the kernel (in this case IPython):

    • may not be running from single file
    • even if one file, the file may not be a notebook.
    • even if notebook, the notebook may not be on a filesystem.
    • even if on a file system, it may not be on the same machine.
    • even if on the same machine the path to the file may not make sens in the IPython context.
    • even if it make sens the Jupyter Protocol has not been designed to do so. And we have no plan to change this abstraction in short or long term.

    Your hack works in most cases and is not too bad depending on the situation.

    0 讨论(0)
提交回复
热议问题