Nameerror: name ‘__file__’ is not defined

The error message nameerror: name ‘__file__’ is not defined raised when you use the file global variable in an interactive shell.

In addition to that, the error occurs when Python cannot find the file variable it raises the name __file__ is not defined error.

The file variable is a built-in variable in Python that represents the name of the current file.

The reason is that the python Shell don’t detect the current file path __file__.

Why does this error “name __file__ is not defined”?

Here are the common reasons why the error occurs in your Python script:

❌ Using the file variable in the wrong context.

❌ Using the file variable in a script that has not been saved.

❌ If you are using the file variable in a script that has been imported into another script.

How to fix “nameerror: name ‘__file__’ is not defined”?

To fix the name file is not defined, you have to create a Python script and run the script with the python main.py command instead.

Here are the different solutions that you can use to resolve the error.

1. Use os.path.abspath

You can use the os.path.abspath method to get the absolute path of the current file.

You can use the following command if you need to access the variable in an interactive shell

import os
result = os.path.abspath(__file__)
print(result)

Output:

C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py

Solution 2: Create a Python script

You can make a file named main.py and after that add the following Python script to your file.

import os

r = os.path.join(os.path.dirname(__file__))

print(r)

Output:

C:\Users\pies-pc2\PycharmProjects\pythonProject

After that, you may now run your code with the python main.py command rather than using the interactive shell.

Then in your interactive shell input exit() function.

Finally, you can now run your program in the python main.py command.

If you are using Python 2:

✅ python main.py

If you are using Python 3:

✅ python3 main.py

If you are using py alias (Windows):

✅ py main.py

Solution 3: Use inspect module

You can also use the inspect module to get the filename of the current file.

For example:

 import inspect

current_file = inspect.getfile(inspect.currentframe())
print(current_file)

or

✅ import inspect
current_file = inspect.getfile(lambda: None)
print(current_file)

Still, the output is the same.

Output:

C:\Users\pies-pc2\PycharmProjects\pythonProject\main.py

Solution 4: Use os.getcwd() to get current file directory

Alternatively, you can use the os.getcwd to get the current working directory.

import os
cwd = os.getcwd()
print(cwd)

Output:

C:\Users\pies-pc2\PycharmProjects\pythonProject

Conclusion

The error message nameerror: name ‘__file__’ is not defined raised when you use the __file__ global variable in an interactive shell.

This article already provides solutions that will help you to fix the Python error message.

You could also check out other “nameerror” articles that may help you in the future if you encounter them.

Hoping that this article helps you fix the error. Thank you for reading itsourcecoders 😊