__file__ does not exist in Jupyter Notebook

前端 未结 4 2136
无人及你
无人及你 2020-12-23 18:54

I\'m on a Jupyter Notebook server (v4.2.2) with Python 3.4.2 and I want to use the global name __file__, because the notebook will be cloned from other users an

4条回答
  •  感动是毒
    2020-12-23 19:22

    It's not possible to get the path to the notebook. You may find a way to get it that only works in one environment (eg os.getcwd()), but it won't necessarily work if the notebook is loaded in a different way.

    Instead, try to write the notebook so that it doesn't need to know its own path. If doing something like getting the pwd, then be sure to fail fast / print an error if this doesn't work, vs. just silently trying to continue on.

    See also: https://github.com/ipython/ipython/issues/10123

提交回复
热议问题