Gramps Portable and relative paths

I’m using Gramps Portable AIO64-6.0.8–1 on Windows and the first bug I found was that the pictures get lost when copying the complete GrampsPortable path content. Pictures are by default stored with an absolute path. Somebody made a small tool media_manager to convert all absolute paths into relative paths. But the Output is this “..\..\..\project\Genealogie\GrampsPortable\Data\mypicture.jpg”, which is not really relative to the \GrampsPortable folder. It finally does not work! Some other guy in the forum suggested using/modifying system variables and ini files with a bat file. To be honest - this solution is not contemporary and … I haven’t fully understood.

Does somebody have a solution for Gramps Portable? Or can somebody point me to the proper source file, so that I can fix this?

BR Marten

What’s actually happening

The Media Manager’s “Convert to relative” op lives in gramps/plugins/tool/mediamanager.py:635-668 (Convert2Rel). It does:

base_dir = media_path(self.db)
...
new_path = relative_path(obj.path, base_dir)

media_path() (gramps/gen/utils/file.py:178-183) returns whatever is set as the “Base media path” for that family tree — a per-tree setting in Edit → Preferences → Family Tree tab (gramps/gui/configure.py:2537-2558). If that field is empty, it falls back (expand_media_path, gramps/gen/utils/file.py:186-208) to your Pictures folder or your user home directory — not to the GrampsPortable folder.

So “relative” in Gramps means relative to that Base media path setting, not relative to wherever GrampsPortable happens to sit. If that setting was empty or pointed somewhere outside the portable folder (e.g. your real Windows user profile), relative_path() (gramps/gen/utils/file.py:112-137) computed a path that climbs back out to a common ancestor and back down — hence the ......\project\Genealogie\GrampsPortable\Data\mypicture.jpg mess. That anchor point doesn’t move when you copy the GrampsPortable folder elsewhere, so it breaks.

The fix

  1. Keep all your media in one folder that travels with the tree — you’re already doing this with GrampsPortable\Data.
  2. Go to Edit → Preferences → Family Tree tab, and in “Base media path” type (don’t just use the folder browser, which inserts an absolute path) a relative path to your media folder, e.g. just Data (or ..\Data, depending on layout).
  • This works because expand_media_path explicitly resolves a relative Base media path relative to the database’s own storage location (db.get_save_path()), not relative to some fixed absolute location. Comment right in the source: “Relative mediapath are considered as relative to the database.”
  1. Now run Tools → Utilities → Media Manager → “Convert paths from absolute to relative” once. Each media object’s path becomes relative to that base folder (e.g. just mypicture.jpg), computed via relative_path(obj.path, base_dir).
  2. From then on, copying the entire GrampsPortable folder anywhere (new drive letter, new PC) keeps working, because both the database location and the Base media path move together and the relative math never touches an absolute anchor.

One caveat that’s outside Gramps’ own source: this only holds if the PortableApps.com launcher is correctly redirecting Gramps’ settings/database home to live inside the GrampsPortable folder (that’s what makes it “portable” in the first place). If it’s silently falling back to your real Windows profile for db.get_save_path(), that’s a GrampsPortable packaging/launcher issue, not something fixable in gramps/ core.

Thank you for this very extensive answer.
I did this:
Edit → Preferences → Family Tree tab, and in “Base media path”
I tried here Data, /Data, \Data, ..\Data, ..\..\Data and ..\..\..\Data
I modified one picture path to just mypicture.jpg
It does not work. Also the media_manager does not recognize these relative Data paths. Have you tried it on your PC?
The funny thing is, that the SQL database IS already relative, the pictures are not.
Isn’t there something like {HOME} or {base_dir} that I can fill in as Base media path?
BR Marten

OK, I have the solution. It seems at the portable version the database path is automatically set at start of the executabe. If I derive the picture path from this database path, then it works. This hint was also mentioned by you @dsblank . So if I set Edit → Preferences → Family Tree tab → Base media path to “..\..\..\” everything works as expected. :grinning_face: