README.rst 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. The problem
  2. ===========
  3. .. image:: https://badge.fury.io/py/platformdirs.svg
  4. :target: https://badge.fury.io/py/platformdirs
  5. .. image:: https://img.shields.io/pypi/pyversions/platformdirs.svg
  6. :target: https://pypi.python.org/pypi/platformdirs/
  7. .. image:: https://github.com/tox-dev/platformdirs/actions/workflows/check.yaml/badge.svg
  8. :target: https://github.com/platformdirs/platformdirs/actions
  9. .. image:: https://static.pepy.tech/badge/platformdirs/month
  10. :target: https://pepy.tech/project/platformdirs
  11. When writing desktop application, finding the right location to store user data
  12. and configuration varies per platform. Even for single-platform apps, there
  13. may by plenty of nuances in figuring out the right location.
  14. For example, if running on macOS, you should use::
  15. ~/Library/Application Support/<AppName>
  16. If on Windows (at least English Win) that should be::
  17. C:\Documents and Settings\<User>\Application Data\Local Settings\<AppAuthor>\<AppName>
  18. or possibly::
  19. C:\Documents and Settings\<User>\Application Data\<AppAuthor>\<AppName>
  20. for `roaming profiles <https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-vista/cc766489(v=ws.10)>`_ but that is another story.
  21. On Linux (and other Unices), according to the `XDG Basedir Spec`_, it should be::
  22. ~/.local/share/<AppName>
  23. .. _XDG Basedir Spec: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
  24. ``platformdirs`` to the rescue
  25. ==============================
  26. This kind of thing is what the ``platformdirs`` package is for.
  27. ``platformdirs`` will help you choose an appropriate:
  28. - user data dir (``user_data_dir``)
  29. - user config dir (``user_config_dir``)
  30. - user cache dir (``user_cache_dir``)
  31. - site data dir (``site_data_dir``)
  32. - site config dir (``site_config_dir``)
  33. - user log dir (``user_log_dir``)
  34. - user documents dir (``user_documents_dir``)
  35. - user downloads dir (``user_downloads_dir``)
  36. - user pictures dir (``user_pictures_dir``)
  37. - user videos dir (``user_videos_dir``)
  38. - user music dir (``user_music_dir``)
  39. - user desktop dir (``user_desktop_dir``)
  40. - user runtime dir (``user_runtime_dir``)
  41. And also:
  42. - Is slightly opinionated on the directory names used. Look for "OPINION" in
  43. documentation and code for when an opinion is being applied.
  44. Example output
  45. ==============
  46. On macOS:
  47. .. code-block:: pycon
  48. >>> from platformdirs import *
  49. >>> appname = "SuperApp"
  50. >>> appauthor = "Acme"
  51. >>> user_data_dir(appname, appauthor)
  52. '/Users/trentm/Library/Application Support/SuperApp'
  53. >>> site_data_dir(appname, appauthor)
  54. '/Library/Application Support/SuperApp'
  55. >>> user_cache_dir(appname, appauthor)
  56. '/Users/trentm/Library/Caches/SuperApp'
  57. >>> user_log_dir(appname, appauthor)
  58. '/Users/trentm/Library/Logs/SuperApp'
  59. >>> user_documents_dir()
  60. '/Users/trentm/Documents'
  61. >>> user_downloads_dir()
  62. '/Users/trentm/Downloads'
  63. >>> user_pictures_dir()
  64. '/Users/trentm/Pictures'
  65. >>> user_videos_dir()
  66. '/Users/trentm/Movies'
  67. >>> user_music_dir()
  68. '/Users/trentm/Music'
  69. >>> user_desktop_dir()
  70. '/Users/trentm/Desktop'
  71. >>> user_runtime_dir(appname, appauthor)
  72. '/Users/trentm/Library/Caches/TemporaryItems/SuperApp'
  73. On Windows:
  74. .. code-block:: pycon
  75. >>> from platformdirs import *
  76. >>> appname = "SuperApp"
  77. >>> appauthor = "Acme"
  78. >>> user_data_dir(appname, appauthor)
  79. 'C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp'
  80. >>> user_data_dir(appname, appauthor, roaming=True)
  81. 'C:\\Users\\trentm\\AppData\\Roaming\\Acme\\SuperApp'
  82. >>> user_cache_dir(appname, appauthor)
  83. 'C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp\\Cache'
  84. >>> user_log_dir(appname, appauthor)
  85. 'C:\\Users\\trentm\\AppData\\Local\\Acme\\SuperApp\\Logs'
  86. >>> user_documents_dir()
  87. 'C:\\Users\\trentm\\Documents'
  88. >>> user_downloads_dir()
  89. 'C:\\Users\\trentm\\Downloads'
  90. >>> user_pictures_dir()
  91. 'C:\\Users\\trentm\\Pictures'
  92. >>> user_videos_dir()
  93. 'C:\\Users\\trentm\\Videos'
  94. >>> user_music_dir()
  95. 'C:\\Users\\trentm\\Music'
  96. >>> user_desktop_dir()
  97. 'C:\\Users\\trentm\\Desktop'
  98. >>> user_runtime_dir(appname, appauthor)
  99. 'C:\\Users\\trentm\\AppData\\Local\\Temp\\Acme\\SuperApp'
  100. On Linux:
  101. .. code-block:: pycon
  102. >>> from platformdirs import *
  103. >>> appname = "SuperApp"
  104. >>> appauthor = "Acme"
  105. >>> user_data_dir(appname, appauthor)
  106. '/home/trentm/.local/share/SuperApp'
  107. >>> site_data_dir(appname, appauthor)
  108. '/usr/local/share/SuperApp'
  109. >>> site_data_dir(appname, appauthor, multipath=True)
  110. '/usr/local/share/SuperApp:/usr/share/SuperApp'
  111. >>> user_cache_dir(appname, appauthor)
  112. '/home/trentm/.cache/SuperApp'
  113. >>> user_log_dir(appname, appauthor)
  114. '/home/trentm/.local/state/SuperApp/log'
  115. >>> user_config_dir(appname)
  116. '/home/trentm/.config/SuperApp'
  117. >>> user_documents_dir()
  118. '/home/trentm/Documents'
  119. >>> user_downloads_dir()
  120. '/home/trentm/Downloads'
  121. >>> user_pictures_dir()
  122. '/home/trentm/Pictures'
  123. >>> user_videos_dir()
  124. '/home/trentm/Videos'
  125. >>> user_music_dir()
  126. '/home/trentm/Music'
  127. >>> user_desktop_dir()
  128. '/home/trentm/Desktop'
  129. >>> user_runtime_dir(appname, appauthor)
  130. '/run/user/{os.getuid()}/SuperApp'
  131. >>> site_config_dir(appname)
  132. '/etc/xdg/SuperApp'
  133. >>> os.environ["XDG_CONFIG_DIRS"] = "/etc:/usr/local/etc"
  134. >>> site_config_dir(appname, multipath=True)
  135. '/etc/SuperApp:/usr/local/etc/SuperApp'
  136. On Android::
  137. >>> from platformdirs import *
  138. >>> appname = "SuperApp"
  139. >>> appauthor = "Acme"
  140. >>> user_data_dir(appname, appauthor)
  141. '/data/data/com.myApp/files/SuperApp'
  142. >>> user_cache_dir(appname, appauthor)
  143. '/data/data/com.myApp/cache/SuperApp'
  144. >>> user_log_dir(appname, appauthor)
  145. '/data/data/com.myApp/cache/SuperApp/log'
  146. >>> user_config_dir(appname)
  147. '/data/data/com.myApp/shared_prefs/SuperApp'
  148. >>> user_documents_dir()
  149. '/storage/emulated/0/Documents'
  150. >>> user_downloads_dir()
  151. '/storage/emulated/0/Downloads'
  152. >>> user_pictures_dir()
  153. '/storage/emulated/0/Pictures'
  154. >>> user_videos_dir()
  155. '/storage/emulated/0/DCIM/Camera'
  156. >>> user_music_dir()
  157. '/storage/emulated/0/Music'
  158. >>> user_desktop_dir()
  159. '/storage/emulated/0/Desktop'
  160. >>> user_runtime_dir(appname, appauthor)
  161. '/data/data/com.myApp/cache/SuperApp/tmp'
  162. Note: Some android apps like Termux and Pydroid are used as shells. These
  163. apps are used by the end user to emulate Linux environment. Presence of
  164. ``SHELL`` environment variable is used by Platformdirs to differentiate
  165. between general android apps and android apps used as shells. Shell android
  166. apps also support ``XDG_*`` environment variables.
  167. ``PlatformDirs`` for convenience
  168. ================================
  169. .. code-block:: pycon
  170. >>> from platformdirs import PlatformDirs
  171. >>> dirs = PlatformDirs("SuperApp", "Acme")
  172. >>> dirs.user_data_dir
  173. '/Users/trentm/Library/Application Support/SuperApp'
  174. >>> dirs.site_data_dir
  175. '/Library/Application Support/SuperApp'
  176. >>> dirs.user_cache_dir
  177. '/Users/trentm/Library/Caches/SuperApp'
  178. >>> dirs.user_log_dir
  179. '/Users/trentm/Library/Logs/SuperApp'
  180. >>> dirs.user_documents_dir
  181. '/Users/trentm/Documents'
  182. >>> dirs.user_downloads_dir
  183. '/Users/trentm/Downloads'
  184. >>> dirs.user_pictures_dir
  185. '/Users/trentm/Pictures'
  186. >>> dirs.user_videos_dir
  187. '/Users/trentm/Movies'
  188. >>> dirs.user_music_dir
  189. '/Users/trentm/Music'
  190. >>> dirs.user_desktop_dir
  191. '/Users/trentm/Desktop'
  192. >>> dirs.user_runtime_dir
  193. '/Users/trentm/Library/Caches/TemporaryItems/SuperApp'
  194. Per-version isolation
  195. =====================
  196. If you have multiple versions of your app in use that you want to be
  197. able to run side-by-side, then you may want version-isolation for these
  198. dirs::
  199. >>> from platformdirs import PlatformDirs
  200. >>> dirs = PlatformDirs("SuperApp", "Acme", version="1.0")
  201. >>> dirs.user_data_dir
  202. '/Users/trentm/Library/Application Support/SuperApp/1.0'
  203. >>> dirs.site_data_dir
  204. '/Library/Application Support/SuperApp/1.0'
  205. >>> dirs.user_cache_dir
  206. '/Users/trentm/Library/Caches/SuperApp/1.0'
  207. >>> dirs.user_log_dir
  208. '/Users/trentm/Library/Logs/SuperApp/1.0'
  209. >>> dirs.user_documents_dir
  210. '/Users/trentm/Documents'
  211. >>> dirs.user_downloads_dir
  212. '/Users/trentm/Downloads'
  213. >>> dirs.user_pictures_dir
  214. '/Users/trentm/Pictures'
  215. >>> dirs.user_videos_dir
  216. '/Users/trentm/Movies'
  217. >>> dirs.user_music_dir
  218. '/Users/trentm/Music'
  219. >>> dirs.user_desktop_dir
  220. '/Users/trentm/Desktop'
  221. >>> dirs.user_runtime_dir
  222. '/Users/trentm/Library/Caches/TemporaryItems/SuperApp/1.0'
  223. Be wary of using this for configuration files though; you'll need to handle
  224. migrating configuration files manually.
  225. Why this Fork?
  226. ==============
  227. This repository is a friendly fork of the wonderful work started by
  228. `ActiveState <https://github.com/ActiveState/appdirs>`_ who created
  229. ``appdirs``, this package's ancestor.
  230. Maintaining an open source project is no easy task, particularly
  231. from within an organization, and the Python community is indebted
  232. to ``appdirs`` (and to Trent Mick and Jeff Rouse in particular) for
  233. creating an incredibly useful simple module, as evidenced by the wide
  234. number of users it has attracted over the years.
  235. Nonetheless, given the number of long-standing open issues
  236. and pull requests, and no clear path towards `ensuring
  237. that maintenance of the package would continue or grow
  238. <https://github.com/ActiveState/appdirs/issues/79>`_, this fork was
  239. created.
  240. Contributions are most welcome.