0

I have been trying to look at the IMDP API through this tsv file https://datasets.imdbws.com/name.basics.tsv.gz". I however I am at a point where I keep on getting this error.

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-6-cfcd2677a0e6> in <module>
----> 1 TsvHandler("https://datasets.imdbws.com/name.basics.tsv.gz")

<ipython-input-4-a2aea9b9f867> in __init__(self, url)
      4         # Example of download url: https://datasets.imdbws.com/title.basics.tsv.gz
      5         self.url = url
----> 6         data_dir = pathlib.Path(__file__).parent.parent.parent.resolve()
      7         self.download_location = os.path.join(data_dir, "data/temp")  # "../../data/temp"
      8         self.filename = self.url.split('/')[-1]

NameError: name '__file__' is not defined
enzo
  • 9,861
  • 3
  • 15
  • 38
  • `__file__` is only meaningful when you're running a script contained in an actual file. You appear to be pasting code into a Python interactive prompt, which inherently has no filename associated with it. – jasonharper Nov 09 '21 at 19:31

1 Answers1

0

First, a Jupyter Notebook doesn't have a __file__ variable, see here.

The first solution would be not running this code in a notebook: try creating a Python file (e.g. filename.py), copy the code you want to run into it and then run it using python filename.py.

In the case you must use a Jupyter Notebook, you have two approaches: if you can change the source code, change __file__ to os.path.abspath(''). Otherwise, before calling the TsvHandler class, try defining a global variable named __file__ and assign it to os.path.abspath(''):

__file__ = os.path.abspath('')
TsvHandler("https://datasets.imdbws.com/name.basics.tsv.gz")
enzo
  • 9,861
  • 3
  • 15
  • 38