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

avatar
Borislav Hadzhiev

Last updated: Apr 10, 2024
3 min

banner

# 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 script_name.py command instead.

nameerror name file is not defined

shell
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.

main.py
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 main.py and add the following code to the file.

main.py
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 main.py command instead of using the interactive shell.

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

shell
exit()

exit interactive session

Now run your Python module with the python main.py command.

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

python run main py script

When you load a module in Python, the global __file__ variable is set to its path.

main.py
import os # 👇️ /home/borislav/Desktop/bobbyhadz_python/main.py print(__file__) # 👇️ /home/borislav/Desktop/bobbyhadz_python print(os.path.dirname(__file__)) # 👇️ /home/borislav/Desktop/bobbyhadz_python/another.py print(os.path.join(os.path.dirname(__file__), 'another.py'))

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.

main.py
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.

main.py
import inspect file_path = inspect.getfile(lambda: None) # 👇️ /home/borislav/Desktop/bobbyhadz_python/main.py 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.

main.py
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.

main.py
import sys print(sys.argv[0]) # 👉️ main.py

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. main.py).

For example, here is a file called another.py that imports the main module and makes use of its __file__ attribute.

another.py
import main # 👇️ /home/borislav/Desktop/bobbyhadz_python/main.py print(main.__file__)

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

shell
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.

main.py
import transformers # 👇️ /home/borislav/Desktop/bobbyhadz_python/venv/lib/python3.10/site-packages/transformers/__init__.py 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.