132

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

IonicSolutions
  • 2,559
  • 1
  • 18
  • 31
Fabian
  • 3,139
  • 2
  • 23
  • 49

7 Answers7

118

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.

Shital Shah
  • 63,284
  • 17
  • 238
  • 185
  • 15
    This does not work for me. It gives me "working directory" instead when I run a notebook with papermill. – untidyhair Jul 13 '19 at 20:09
  • That surprising. In my testing all 3 scenario works. Could this because of papermill (I'm not familiar with it)? – Shital Shah Jul 14 '19 at 08:17
  • 8
    @Shital, untidyhair is correct. This, like the other suggestion from mab below, does NOT work. It merely gives you the cwd. – germ Jul 21 '19 at 21:40
47

__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')
shytikov
  • 9,155
  • 8
  • 56
  • 103
  • 6
    Wow, congratulations @shytikov, your method **really works** and should be the preferred answer. You have found a method that avoids the ugly JS hacks. Many, many thanks. Now, if we could get a way to get the notebook name... – germ Jul 21 '19 at 21:32
  • It looks like this works in Notebook, but _not_ in a regular Python interpreter, (at least when imported as a module). I switch between using Jupyter kernels and a regular Python interpreter, so I need something that works on both. I think I'm going to resort to an environment variable. – Kyle Barron Nov 27 '19 at 00:47
  • This works for me both in JupyterLab and Pycharm, while the other suggestions did not. – aimfeld Mar 19 '20 at 09:55
  • why not use `os.getcwd()` in lieu of `globals()['_dh'][0]`? – John Doe Mar 18 '21 at 16:25
  • 2
    This didn't work for me when using papermill to execute the notebook from a different directory. Both `os.getcwd()` and `globals()["_dh"]` showed the directory from which I ran the papermill command, not the directory the notebook template or the directory the executed notebook would land – Ben Lindsay Apr 05 '21 at 16:38
41

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

mab
  • 2,658
  • 26
  • 36
  • 1
    @ShitalShah please see the updated answer. Maybe you can clarify, what is the problem you are experiencing. – mab Jan 24 '19 at 12:24
  • 2
    `Path` has a class method just for that: `Path.cwd()` – Nuno André Jun 05 '19 at 23:01
  • 8
    @mac, this DOES NOT work. If you change cwd in your notebook, then re-execute that cell, it will give you the new cwd, not the notbook's location. This is a sorely missing feature in Jupyter Notebook. I have to resort to a hack involving Javascript. What a PITA. – germ Jul 21 '19 at 21:28
  • @germ do you say the scenario from my updated post is not possible? What do you mean by "if you change cwd in your notebook?" :) – mab Jul 31 '19 at 20:25
  • 2
    @mab: Try `import os;os.chdir('path/to/new/dir')`. If you now try both `Path.cwd()` and `Path().resolve()`, they both point to `path/to/new/dir` and not to the location of your notebook! – germ Aug 11 '19 at 23:43
  • @germ yes you are right, thanks for the clarification. However, this behavior is expected. I'd say one has to save the original working directory location at the start of a notebook as a workaround to ensure there are no previous alterations to the global working directory path. – mab Aug 15 '19 at 18:45
  • 1
    @mab, it is not possible to reliably do that, because the notebook interface allows you to execute cells in arbitrary order--which is a really nice feature BTW. You can save the directory at the beginning of your notebook. However, if you later use `os.chdir`, then re-execute the first cell in your notebook, you will still get the wrong answer. – germ Aug 17 '19 at 02:25
  • @germ not exactly, you can just check the current global variable before reassigning another value. `if not globals().get('a', False): a = Path.cwd()`;. That isn't pretty, but you are tampering with messy global variables already, anyway :) – mab Dec 30 '19 at 13:09
18

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

michael
  • 9,161
  • 2
  • 52
  • 49
  • 2
    you are right that it isn't possible to get the path to the notebook with os.getcwd(). This merely gets the cwd. If you have changed that in the notebook, then go back to that cell, you will not get the notebook directory. THIS IS A HUGE PROBLEM. And no, I disagree that you should write your script such that it doesn't need to know where it is. There are very good reasons to do that, such as making a copy to another location. – germ Jul 21 '19 at 21:25
  • While it may not be straightforward to do what the OP requests, there are solid reasons to find the path. E.g. a bidirectional sync between python and a notebook using `jupytext` in which the path to ancillary files is critical. I'll want to find a reliable solution – WestCoastProjects Feb 12 '23 at 22:53
2

On new version of python and notebook __file__ works... If you have older versions, to get __file__ you can use

import inspect
from pathlib import Path

module_path = Path(inspect.getframeinfo(inspect.currentframe()).filename).resolve()

But... This is much slower... On the other hand, it will not return currrent working directory, but path to module, even if module is imported from elsewhere.

Daniel Malachov
  • 1,604
  • 1
  • 10
  • 13
1

I'm new to python, but this works for me.

You can get os.path.dirname(__file__) equivalence by:

sys.path[0]
Marvin Xu
  • 139
  • 7
0

For Jupyter Notebooks, enclose the file with quotes i.e "file". Then use the dirname method to get the file DIR

import os

THIS_DIR = os.path.dirname(os.path.abspath("__file__"))
print(THIS_DIR)

# /home/brightkoech/Projects/EPL-Streamlit-Visualisation/src
Brian Koech
  • 183
  • 1
  • 5
  • This answer is misleading. It gives the false impression that the string `__file__` has a special meaning to `abspath`. It has not. You can put any string you want there. What `abspath` is doing is just resolving the relative path given as an argument using the current working directory and then `dirname` will extract the dir. So this is completely equivalent to `getcwd`. – Vittorio Ballestra May 26 '23 at 07:59