Get the Path of the Python Script

This article discusses three methods you can use to fetch the path of the current Python file – getting the location of the Python script being executed is very useful if you need to load other files that are relative to the location of the script.

The methods we will discuss include the following:

  • Method 1: Using the os module and the __file__ dunder variable,
  • Method 2: Using os or path modules and inspect the package and,
  • Method 3: Using the os and sys module

In most cases, using the __file__ variable gets the job done, but sometimes, the dunder variable may not be available. We will discuss more in the next section.

Note: The code examples used in this guide have been tested in Python 3.11.3.

Method 1: Using the os Module and the __file__ Variable

The __file__ variable in Python allows you to obtain the path of the currently running Python script. However, it’s worth noting that the output obtained by printing this variable may differ depending on your Python version.

For Python 3.9+, __file__ always returns the absolute path of the current script.

In Python 3.8 and earlier, __file__ returns the relative or absolute path of the current script based on the path specified when calling the Python command.

Consider the get_path2.py file in /Users/kiprono/Desktop/, printing __file__ within the script will yield the following:

Output:

/Users/kiprono/Desktop/get_path2.py

You can use the os package and the __file_variable to fetch the directory containing the script, as shown below.

Output:

/Users/kiprono/Desktop

Note: os.path.realpath() method is used to get the canonical path of the specified filename by eliminating any symbolic links within the path.

If you are running Python 3.8 and before, you can get the absolute path of the current script using os.path.abspath(), as shown below.

Output:

/Users/kiprono/Desktop/get_path2.py

Then you can get the script name using os.basename().

Output:

get_path2.py

Lastly, you can change the current working directory to the path containing the current script using the os.chdir() function, as shown below (add it on top of your script). This will allow you to call the script from any directory or another script and load other files relative to the current file’s location.

Important note

__file__ variable is set by Python in modules loaded from files. The variable is not created when running code in Python shell (calling python/python3 from the terminal), in JuPyter Notebook, or in other cases where the module is not loaded from the file.

That is why we need the other methods.

Method 2: Using os or path and inspect the package

If you can’t load the __file__ variable, you can use the os and inspect the package to get the current script and directory.

Output:

/Users/kiprono/Desktop/get_path2.py
/Users/kiprono/Desktop

Or, use inspect.getframeinfo() function as shown below.

Output:

/Users/kiprono/Desktop/get_path2.py
/Users/kiprono/Desktop

As mentioned in Method 1, based on the Python version you are running, print( __file__ ) may not generate an absolute path for the current file. In that case, you can use the path module as follows.

Output:

/Users/kiprono/Desktop/Get_the_Path_of_the_Python_Script/using_Path.py
/Users/kiprono/Desktop/Get_the_Path_of_the_Python_Script
/Users/kiprono/Desktop/Get_the_Path_of_the_Python_Script/using_Path.py

Method 3: Using os and sys

This method works best if you are compiling your scripts on py2exe.

The sys.argv attribute returns a Python list containing all command-line arguments passed into the current script. The first element, sys.argv[0], is the script name. Therefore, we can get the path of the current Python script, as shown below.

Note: sys.argv[0] is system dependent – it can be a full pathname or just the script name.

Output:

/Users/kiprono/Desktop/get_path2.py
/Users/kiprono/Desktop/get_path2.py

Conclusion

This guide discussed three ways to get the path of the current Python script. You can pick any method based on the platform you use to execute your code.