Last updated: Apr 10, 2024
Reading time·3 min
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.
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name '__file__' is not defined. Did you mean: '__name__'?
__file__
variable in an interactive shellThe 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__'))
The os.path.abspath
method takes a path and returns a normalized, absolute
version of the path.
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.
import os result = os.path.join(os.path.dirname(__file__)) # 👇️ /home/borislav/Desktop/bobbyhadz_python print(result)
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.
exit()
Now run your Python module with the python main.py
command.
python main.py # 👇️ For Python 3 python3 main.py # 👇️ Using py alias (Windows) py main.py
When you load a module in Python, the global __file__
variable is set to its
path.
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'))
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.
inspect
module insteadAn 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/main.py print(file_path)
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.
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())
sys.argv
If you need just the name of the current module, use the sys.argv
list.
import sys print(sys.argv[0]) # 👉️ main.py
The sys.argv list contains the command line arguments that were passed to the Python script.
argv[0]
is the name of the script, argv[1]
is the first provided command line argument, etc.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.
import main # 👇️ /home/borislav/Desktop/bobbyhadz_python/main.py 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/__init__.py print(transformers.__file__)