# \`anki.collection.Collection::export\_anki\_package\` crashes if given a relative path with one component

**URL:** <https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562>\
**Category:** Development\
**Created:** [April 28, 2026, 6:44am UTC](https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562 "2026-04-28T06:44:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfly](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/jfly/32/33582_2.png) [@jfly](https://forums.ankiweb.net/u/jfly)\
**Post date:** [April 28, 2026, 6:44am UTC](https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562/1 "2026-04-28T06:44:08Z")

</div>

(Frustratingly, the forum isn’t allowing me to include links in my post. I’ve attempted to work around that.)

I’m using the `anki` Python module. I found that calling `export_anki_package` crashes if I give it a relative path. Here’s a simple repro.

`demo.py`:

```python
import tempfile
from pathlib import Path

import anki.collection

with tempfile.TemporaryDirectory() as tempdir:
    tempdir = Path(tempdir)
    col = anki.collection.Collection(str(tempdir / "temp.anki2"))
    col.export_anki_package(
        out_path="output.apkg",
        options=anki.collection.ExportAnkiPackageOptions(),
        limit=None,
    )

```

```console
$ python demo.py
Traceback (most recent call last):
  [... some noise elided here ...]
  File "/nix/store/1w7swjzmjp7171yspih1q07a9vgacjzb-dev-env/lib/python3.14/site-packages/anki/_backend_generated.py", line 2037, in export_anki_package
    raw_bytes = self._run_command(37, 4, message.SerializeToString())
  File "/nix/store/1w7swjzmjp7171yspih1q07a9vgacjzb-dev-env/lib/python3.14/site-packages/anki/_backend.py", line 171, in _run_command
    raise backend_exception_to_pylib(err)
anki.errors.BackendIOError: Failed to open '': No such file or directory (os error 2)

```

I dug into this:

- `apkg::Collection::export_apkg` calls `atomic_rename` `[0]` with `target = "output.apkg"`
- `atomic_rename` attempts to open `target`’s `parent` `[1]`, but the parent of a relative path with one component is `""`. From `[2]`:

> This means it returns `Some("")` for relative paths with one component.

The fix seems simple enough: change `atomic_rename` to call `std::path::absolute` on the given target before accessing its parent. I wouldn’t be surprised if this fixes similar bugs in other places that call `atomic_rename`. Happy to send in a PR implementing this if it’s acceptable.

`[0]`: `https://github.com/ankitects/anki/blob/25.09.2/rslib/src/import_export/package/apkg/export.rs#L64`  
`[1]`: `https://github.com/ankitects/anki/blob/25.09.2/rslib/io/src/lib.rs#L265-L266`  
`[2]`: `https://doc.rust-lang.org/std/path/struct.Path.html#method.parent`

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [April 28, 2026, 12:35pm UTC](https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562/2 "2026-04-28T12:35:53Z")

</div>

I suggest limiting this to only the export methods. `atomic_rename` is also used when importing media files, so I think it’s better to avoid an extra system call (on Windows) for each file there if we can: [absolute in std::path - Rust](https://doc.rust-lang.org/std/path/fn.absolute.html)

---

<div class="post-metadata">

**Author:** ![jfly](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/jfly/32/33582_2.png) [@jfly](https://forums.ankiweb.net/u/jfly)\
**Post date:** [April 28, 2026, 2:03pm UTC](https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562/3 "2026-04-28T14:03:39Z")

</div>

> I suggest limiting this to only the export methods.

I can do that, but I still suggest that we make some change to `atomic_rename` to handle this more gracefully. The current behavior is kind of confusing, and took me a bit of `strace`-ing to figure out. A few ideas:

- If `target` is not absolute, raise an error (or call `absolute`).
- If `target.parent() == ""`, raise an error (or call `absolute`).

Of these 4 options, I like these 2 best:

1. Reject non absolute paths: if `target` is not absolute, raise an error.
2. if `target.parent() == ""`, call `absolute`

I slightly prefer 2), as it removes the footgun entirely. 1) seems pretty reasonable as well, though. WDYT?

---

<div class="post-metadata">

**Author:** ![abdo](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/abdo/32/654_2.png) [@abdo](https://forums.ankiweb.net/u/abdo)\
**Post date:** [April 29, 2026, 2:15am UTC](https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562/4 "2026-04-29T02:15:21Z")

</div>

> [@jfly](#):
>
> if `target.parent() == ""`, call `absolute`

This sounds good. My earlier point about avoiding a system call is not valid: The Windows branch doesn’t have the issue.

---

<div class="post-metadata">

**Author:** ![jfly](https://sea2.discourse-cdn.com/flex002/user_avatar/forums.ankiweb.net/jfly/32/33582_2.png) [@jfly](https://forums.ankiweb.net/u/jfly)\
**Post date:** [May 3, 2026, 5:15pm UTC](https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562/5 "2026-05-03T17:15:18Z")

</div>

I’ve sent in a PR fixing this: [fix: `atomic_rename` now works with single component relative paths by jfly · Pull Request #4773 · ankitects/anki · GitHub](https://github.com/ankitects/anki/pull/4773)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex002/uploads/anki2/original/1X/8f1279ababc5879d54e4838989f606cfe55af8c7.jpeg) [@system](https://forums.ankiweb.net/u/system)\
**Post date:** [June 2, 2026, 5:15pm UTC](https://forums.ankiweb.net/t/anki-collection-collection-export-anki-package-crashes-if-given-a-relative-path-with-one-component/69562/6 "2026-06-02T17:15:29Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
