Where should a CLI tool store config per-venv without leaking across environments (original) (raw)

March 6, 2026, 9:15pm 1

Hello,

I have a PyPI package that is a CLI/GUI tool (not a library). I want to store its configuration file in a way that does not conflict with installations in other virtual environments.

In other words, I do not want the tool’s configuration to leak across separate venvs. Ideally, the configuration would also be removed automatically when the package is uninstalled with pip.

Is there a recommended location or pattern for storing configuration in this situation?

inventshah (Sachin S) March 6, 2026, 9:25pm 2

One strategy would be to use importlib.resources and place the config with the code. This data would live inside the venv folder, so no conflicts.

bwoodsend (Brénainn Woodsend) March 7, 2026, 12:18am 3

It depends on what setups you don’t mind breaking. It’s perfectly reasonable for a Python environment to be installed in a read-only location so writing in the package or next to the package or to somewhere like sys.prefix/etc are all bad options for those cases. For GUIs, things like macOS’s sandboxing also restrict where you can write.

I’d suggest something like:

appdirs.user_config_dir("appname", False, hashlib.md5(sys.prefix.encode()).hexdigest())

to ensure writability whilst keeping environments separate.

Unless you forget about pip/PyPI and write an installer with a corresponding uninstaller that you can add hooks to, I don’t think anything will get config deleted on uninstall.