Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add __file__ variable supported by traditional Python scripts to Notebook environment #227

Open
palewire opened this issue Feb 7, 2017 · 2 comments

Comments

@palewire
Copy link

palewire commented Feb 7, 2017

It would be cool, if the __file__ variable present in traditional Python scripts also existed with a notebook. This could be especially helpful when generating dynamic paths.

Other Python double underscore methods, such as __doc__ and __name__, are already present. Why not add this one?

@mgeier
Copy link

mgeier commented Feb 4, 2018

FWIW, a traditional interactive Python session doesn't define __file__ either.

I guess the kernel isn't associated with any file. It's just started and waits for Jupyter clients to connect. It doesn't even necessarily have to know that one of the clients happens to be a Jupyter Notebook which happens to have physical file associated with it.

If you want to get the notebook name from within the running Notebook, you can try to get it via JavaScript. In the JavaScript namespace there seem to be several related things (and probably more?):

  • IPython.notebook.notebook_path
  • IPython.notebook.notebook_name
  • IPython.notebook.get_notebook_name()

@geyang
Copy link

geyang commented Mar 8, 2020

This request has been kicked around quite a bit in the past. This requires client-side information. Implementing this as a cell magic would pretty clean and sensible.

%__file__
print(__file__)
# >>> "uvpn_experiments/icml_2020/local_metric_analysis.ipynb"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants