How to Read a Data File Inside a Python Package with importlib.resources
A program often ships a file with its code: a template, a list, a message. open("message.txt") looks for it in whatever folder you happen to run from, so it breaks as soon as you run from somewhere else. importlib.resources looks inside the package instead. This guide reads a text file that way, with real output from Python 3.15.2.
Syntax
from importlib.resources import files
text = files("<package>").joinpath("<file name>").read_text(encoding="utf-8")
| Part | What it means |
|---|---|
files("<package>") | The folder of the importable package named <package> |
.joinpath("<file name>") | A file inside that folder |
.read_text(encoding="utf-8") | Reads the file and returns its text |
Python's importlib.resources documentation: files() "Returns a Traversable object representing the resource container (think directory) and its resources (think files)." If the anchor "is a package, resources are resolved from that package."
Prerequisites
- uv and Python. This guide uses uv 0.13.10 with Python 3.15.2.
Step 1: Create a Package
uv init --package makes a project whose code lives in a package, with a command to run it:
uv init --package --python 3.15 greeter
Initialized project `greeter` at `/Users/you/source/greeter`
Save one line of text as src/greeter/message.txt, next to the package's __init__.py:
Hello from a file inside the greeter package!
The project now looks like this:
greeter/
├── .gitignore
├── .python-version
├── README.md
├── pyproject.toml
└── src/
└── greeter/
├── __init__.py
└── message.txt
Step 2: Read the File in the Code
Replace src/greeter/__init__.py with:
from importlib.resources import files
def main() -> None:
message = files("greeter").joinpath("message.txt").read_text(encoding="utf-8")
print(message)

uv init --package already added a greeter command that calls main(), in pyproject.toml under [project.scripts].
Step 3: Run It from Two Folders
From the project folder, then from the folder above it:
uv run greeter
cd ..
uv run --project greeter greeter
Hello from a file inside the greeter package!
Hello from a file inside the greeter package!

Both runs found the file, because files("greeter") points at the package's own folder, wherever you start from. The first uv run also creates the project's environment and installs the package; uv's build backend includes message.txt with the code.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Reading it with open("message.txt") | From the project folder: FileNotFoundError: [Errno 2] No such file or directory: 'message.txt', because the file is in src/greeter/, not the current folder | Use files("greeter").joinpath("message.txt") |
| Misspelling the package name | ModuleNotFoundError: No module named 'wrong_name' | Use the import name of the package, here greeter |
Leaving out encoding="utf-8" | The system's default encoding is used, which differs between computers | Always pass the encoding |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
FileNotFoundError naming the file inside the package | The file isn't in the package folder, or has another name | Put it next to __init__.py and check the name |
FAQ
Can the file be in a subfolder?
Yes: files("greeter").joinpath("data", "message.txt") points at src/greeter/data/message.txt.
What about binary files, such as images?
Use .read_bytes() instead of .read_text().
Conclusion
files("greeter").joinpath("message.txt").read_text(encoding="utf-8") found the package's file from the project folder and from the folder above, where a plain open() failed.
Additional Resources
- importlib.resources —
files()and theTraversableit returns.
