Skip to main content

How to Read a Data File Inside a Python Package with importlib.resources

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
from importlib.resources import files

text = files("<package>").joinpath("<file name>").read_text(encoding="utf-8")
PartWhat 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:

Terminal
uv init --package --python 3.15 greeter
Output
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:

src/greeter/message.txt
Hello from a file inside the greeter package!

The project now looks like this:

greeter/
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:

src/greeter/__init__.py
from importlib.resources import files


def main() -> None:
message = files("greeter").joinpath("message.txt").read_text(encoding="utf-8")
print(message)

The src/greeter/init.py file in a macOS-style code window: main reads message.txt from the greeter package with files, joinpath and read_text, then prints it.

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:

Terminal
uv run greeter
cd ..
uv run --project greeter greeter
Output
Hello from a file inside the greeter package!
Hello from a file inside the greeter package!

The output in a terminal: uv run greeter in the project folder and uv run --project greeter greeter from the folder above both print 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​

MistakeWhat happensDo 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 folderUse files("greeter").joinpath("message.txt")
Misspelling the package nameModuleNotFoundError: 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 computersAlways pass the encoding

Troubleshooting​

SymptomLikely causeFix
FileNotFoundError naming the file inside the packageThe file isn't in the package folder, or has another namePut 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​