Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__file__ does not exist in Jupyter Notebook

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 and in one section I have to run:

def __init__(self, trainingSamplesFolder='samples', maskFolder='masks'):     self.trainingSamplesFolder = self.__getAbsPath(trainingSamplesFolder)     self.maskFolder = self.__getAbsPath(maskFolder)  def __getAbsPath(self, path):     if os.path.isabs(path):         return path     else:         return os.path.join(os.path.dirname(__file__), path) 

The __getAbsPath(self, path) checks if a path param is a relative or absolute path and returns the absolute version of path. So I can use the returned path safely later.

But I get the error

NameError: name '__file__' is not defined

I searched for this error online and found the "solution" that I should better use sys.argv[0], but print(sys.argv[0]) returns

/usr/local/lib/python3.4/dist-packages/ipykernel/__main__.py

But the correct notebook location should be /home/ubuntu/notebooks/.

Thanks for the reference How do I get the current IPython Notebook name from Martijn Pieters (comments) the last answer (not accepted) fits perfect for my needs:

print(os.getcwd())

/home/ubuntu/notebooks

like image 449
Fabian Avatar asked Aug 24 '16 14:08

Fabian


People also ask

What does __ file __ mean in Python?

__file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

How do you fix a name error in Jupyter Notebook?

To solve this issue you have to just run the cell first that has import pandas as pd statement. Then run the other cell. It will clearly remove the nameerror name pd is not defined error. You can see you are now not getting any error.


2 Answers

If you want to get path of the directory in which your script is running, I would highly recommend using,

os.path.abspath('') 

Advantages

  • It works from Jupyter Notebook
  • It work from REPL
  • It doesn't require Python 3.4's pathlib

Please note that one scenario where __file__ has advantage is when you are invoking python from directory A but running script in directory B. In that case above as well as most other methods will return A, not B. However for Jupyter notbook, you always get folder for .ipyn file instead of the directory from where you launched jupyter notebook.

like image 130
Shital Shah Avatar answered Sep 30 '22 21:09

Shital Shah


__file__ might not be available for you, but you can get current folder in which your notebook is located in different way, actually.

There are traces in global variables, if you will call globals() you will see that there is an element with the key _dh, that might help you. Here how I managed to load the data.csv file that is located in the same folder as my notebook:

import os  current_folder = globals()['_dh'][0]  # Calculating path to the input data data_location = os.path.join(current_folder,'data.csv') 
like image 37
shytikov Avatar answered Sep 30 '22 19:09

shytikov