__file__ does not exist in Jupyter Notebook

Python 3.xPathJupyter

Python 3.x Problem Overview


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 https://stackoverflow.com/questions/12544056/how-to-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

Python 3.x Solutions


Solution 1 - Python 3.x

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.

Solution 2 - Python 3.x

__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')

Solution 3 - Python 3.x

In modern Python (v3.4+) we can use pathlib to get the notebook's directory:

from pathlib import Path

cwd = Path().resolve()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')

# or this way, thanks @NunoAndré:
cwd = Path.cwd()
# cwd == PosixPath('/path/to/this/jupyter/ipynb/file's/directory/')



Update

@ShitalShah I cannot reproduce the error you are reporting. Jupyter notebook seems to work fine, regardless of the current working directory the application was started.

Example: file ~/dir1/dir2/untitled.ipynb and Jupyter notebook started in ~/dir1:

Jupyter notebook started in ~/dir1

Jupyter notebook started in ~/dir1/dir2:

Jupyter notebook started in ~/dir1/dir2

Solution 4 - Python 3.x

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFabianView Question on Stackoverflow
Solution 1 - Python 3.xShital ShahView Answer on Stackoverflow
Solution 2 - Python 3.xshytikovView Answer on Stackoverflow
Solution 3 - Python 3.xmabView Answer on Stackoverflow
Solution 4 - Python 3.xmichaelView Answer on Stackoverflow