NameError: name '__file__' is not defined in Python [Fixed]

Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min


# NameError: name '__file__' is not defined' in Python

The "NameError: name '__file__' is not defined'" occurs when we use the __file__ global variable in an interactive shell.

To solve the error, create a Python script and run the script with the python command instead.

nameerror name file is not defined

Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name '__file__' is not defined. Did you mean: '__name__'?

# Trying to access the __file__ variable in an interactive shell

The most common cause of the error is trying to access the __file__ global variable in an interactive shell.

If you need to access the variable in an interactive shell, try the following code sample.
import os # 👇️ '/home/borislav/Desktop/bobbyhadz_python' os.path.dirname(os.path.abspath('__file__'))

access file variable in interactive shell

The os.path.abspath method takes a path and returns a normalized, absolute version of the path.

# Storing your code in a Python module instead

However, a much better approach is to store your code in a Python module.

Create a file named and add the following code to the file.
import os result = os.path.join(os.path.dirname(__file__)) # 👇️ /home/borislav/Desktop/bobbyhadz_python print(result)

access file from python script

Now you can run your code with the python command instead of using the interactive shell.

You can exit the interactive shell with the exit() function.


exit interactive session

Now run your Python module with the python command.

python # 👇️ For Python 3 python3 # 👇️ Using py alias (Windows) py

python run main py script

When you load a module in Python, the global __file__ variable is set to its path.
import os # 👇️ /home/borislav/Desktop/bobbyhadz_python/ print(__file__) # 👇️ /home/borislav/Desktop/bobbyhadz_python print(os.path.dirname(__file__)) # 👇️ /home/borislav/Desktop/bobbyhadz_python/ print(os.path.join(os.path.dirname(__file__), ''))

loading a module

The os.path.dirname method returns the directory component of a pathname.

The os.path.join method joins one or more paths intelligently.
import os # 👇️ /home/bobbyhadz print(os.path.join('/', 'home', 'bobbyhadz'))

The method concatenates the provided paths with exactly one directory separator following each non-empty part except the last.

If any of the provided components is an absolute path, all previous components are thrown away and joining continues from the absolute path onwards.

# Using the inspect module instead

An alternative approach is to use the inspect module to get the path to the current file.
import inspect file_path = inspect.getfile(lambda: None) # 👇️ /home/borislav/Desktop/bobbyhadz_python/ print(file_path)

using inspect module instead

The inspect.getfile() method returns the absolute path to the module in which the lambda function is defined.

In other words, it returns the absolute path to the current module.

# Getting the current working directory with os.getcwd()

You can use the os.getcwd() method if you need to get the current working directory.
import os # 👇️ '/home/borislav/Desktop/bobbyhadz_python' print(os.getcwd())

get current working directory

# Getting the name of the module with sys.argv

If you need just the name of the current module, use the sys.argv list.
import sys print(sys.argv[0]) # 👉️

using sys argv to get file name

The sys.argv list contains the command line arguments that were passed to the Python script.

Where argv[0] is the name of the script, argv[1] is the first provided command line argument, etc.

# Accessing the attribute on imported modules

The __file__ attribute is also available on modules you import into your Python script (e.g.

For example, here is a file called that imports the main module and makes use of its __file__ attribute.
import main # 👇️ /home/borislav/Desktop/bobbyhadz_python/ print(main.__file__)

Another example would be to install a third-party module.

pip install transformers pip3 install transformers

Now I can import the transformers module and access its __file__ attribute to get the path to the file where the module is stored.
import transformers # 👇️ /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/transformers/ print(transformers.__file__)
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.