For a file-backed Python module, use Path(__file__).parent to get its containing directory. Use Path(__file__).resolve().parent when you need an absolute path with symlinks resolved and .. components removed. These paths are anchored to the module, not the process’s current working directory.
Get the directory containing the current Python file
Use the standard-library pathlib module:
from pathlib import Path
module_dir = Path(__file__).parent
parent returns the directory component of the path as represented. It is a lexical operation: it does not inspect the filesystem, resolve symlinks, or remove .. components. [Python pathlib documentation]
As an Amazon Associate I earn from qualifying purchases.
Choose the path operation you need
| What you need | Use | What it does |
|---|---|---|
| The containing directory as represented by the module path | Path(__file__).parent |
Returns the lexical parent; it does not resolve symlinks or normalize ... [Python pathlib documentation] |
| An absolute path, without resolving symlinks | Path(__file__).absolute() |
Makes the path absolute without normalization or symlink resolution. [Python pathlib documentation] |
An absolute path with symlinks resolved and .. removed |
Path(__file__).resolve().parent |
resolve() makes the path absolute, follows symlinks, and removes .. components. [Python pathlib documentation] |
| The process’s current working directory | Path.cwd() |
Returns the current working directory, which is distinct from the directory containing a module. [Python pathlib documentation] |
Build a path to a nearby resource
To locate a file stored beside the current module, join its name to the module directory with the / operator:
Recommended Free Tools
from pathlib import Path
module_dir = Path(__file__).resolve().parent
config_file = module_dir / "settings.json"
Here, module_dir is based on the module’s pathname; it does not depend on where the process was launched. If you want to refer directly to a file with the same parent as the module, use Path(__file__).resolve().with_name("settings.json"). [Python pathlib documentation]
#1 Best Overall
Why the current working directory is different
Path.cwd() answers where the process is running, not where a particular Python source file is stored. A relative path is generally interpreted from the current working directory. As the Python documentation explains, “Relative paths are interpreted relative to the current working directory, not the directory of the Path object.” [Python pathlib documentation]
Use Path.cwd() when you need the process’s working directory. Use a path derived from __file__ when the resource is intended to be located beside a module.
Rank #2
When __file__ may not be available
__file__ is module metadata, not a universal Python global. The Python 3.14.8 data model describes it as optional: when present, it identifies the pathname from which the module was loaded, but some module types or loaders without a meaningful pathname may not provide it. Code that can run in such a context should be given or derive an appropriate base path instead of assuming __file__ exists. [Python data model documentation]
Quick wins for a faster PC:
Repair Windows errors before they cause bigger problemsFix Now →Fix the driver behind crashes, sound loss and screen glitchesFind Drivers →Clear out junk files and repair common Windows errorsFree Scan →Using module-relative paths in a PyInstaller bundle
PyInstaller 6.8.0 documents that its bootloader sets __file__ for bundled modules and recommends resolving it to locate sibling data files. Its documented pattern is Path(__file__).resolve().with_name("other-file.dat"). [PyInstaller runtime information]
What resolve() does—and does not—guarantee
Path.resolve() makes a path absolute, resolves symlinks, and removes .. components. With non-strict behavior, it resolves as far as possible and appends any remaining path components without checking that they exist. A resolved path therefore should not be treated as proof that its target exists. [Python pathlib documentation]
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




