Skip to content

Commit 16cd0b3

Browse files
author
jaseg
committed
Windows: Look for mpv.dll next to mpv.py
1 parent 3d09f51 commit 16cd0b3

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

README.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ submit a `pull request`_ on github.
2929

3030
On Windows you can place libmpv anywhere in your ``%PATH%`` (e.g. next to ``python.exe``) or next to this module's
3131
``mpv.py``. Before falling back to looking in the mpv module's directory, python-mpv uses the DLL search order built
32-
into ctypes, which is different to the one Windows uses internally. Consult `this stackoverflow post
33-
<https://stackoverflow.com/a/23805306>`__ for details.
32+
into ctypes, which is different to the one Windows uses internally. You can modify `%PATH%` before importing python-mpv
33+
to modify where python-mpv looks for the DLL. Consult `this stackoverflow post <https://stackoverflow.com/a/23805306>`__
34+
for details.
3435

3536
Python >= 3.9
3637
.............

mpv.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# vim: ts=4 sw=4 et
33
#
44
# Python MPV library module
5-
# Copyright (C) 2017-2022 Sebastian Götte <[email protected]>
5+
# Copyright (C) 2017-2024 Sebastian Götte <[email protected]>
66
#
77
# python-mpv inherits the underlying libmpv's license, which can be either GPLv2 or later (default) or LGPLv2.1 or
88
# later. For details, see the mpv copyright page here: https://github.com/mpv-player/mpv/blob/master/Copyright
@@ -24,6 +24,7 @@
2424
import threading
2525
import queue
2626
import os
27+
import os.path
2728
import sys
2829
from warnings import warn
2930
from functools import partial, wraps
@@ -35,19 +36,30 @@
3536

3637
if os.name == 'nt':
3738
# Note: mpv-2.dll with API version 2 corresponds to mpv v0.35.0. Most things should work with the fallback, too.
38-
dll = ctypes.util.find_library('mpv-2.dll') or ctypes.util.find_library('libmpv-2.dll') or ctypes.util.find_library('mpv-1.dll')
39-
if dll is None:
40-
raise OSError('Cannot find mpv-1.dll, mpv-2.dll or libmpv-2.dll in your system %PATH%. One way to deal with this is to ship the dll with your script and put the directory your script is in into %PATH% before "import mpv": os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"] If mpv-1.dll is located elsewhere, you can add that path to os.environ["PATH"].')
41-
# flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
42-
# cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
39+
names = ['mpv-2.dll', 'libmpv-2.dll', 'mpv-1.dll']
40+
for name in names:
41+
dll = ctypes.util.find_library(name)
42+
if dll:
43+
break
44+
else:
45+
for name in names:
46+
dll = os.path.join(os.path.dirname(__file__), name)
47+
if os.path.isfile(dll):
48+
break
49+
else:
50+
raise OSError('Cannot find mpv-1.dll, mpv-2.dll or libmpv-2.dll in your system %PATH%. One way to deal with this is to ship the dll with your script and put the directory your script is in into %PATH% before "import mpv": os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"] If mpv-1.dll is located elsewhere, you can add that path to os.environ["PATH"].')
51+
4352
try:
53+
# flags argument: LOAD_LIBRARY_SEARCH_DEFAULT_DIRS | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR
54+
# cf. https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa
4455
backend = CDLL(dll, 0x00001000 | 0x00000100)
4556
except Exception as e:
46-
if not os.path.isabs(dll):
57+
if not os.path.isabs(dll): # can only be find_library, not the "look next to mpv.py" thing
4758
raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it. It looks like find_library found mpv.dll under a relative path entry in %PATH%. Please make sure all paths in %PATH% are absolute. Instead of trying to load mpv.dll from the current working directory, put it somewhere next to your script and add that path to %PATH% using os.environ["PATH"] = os.path.dirname(__file__) + os.pathsep + os.environ["PATH"]') from e
4859
else:
4960
raise OSError(f'ctypes.find_library found mpv.dll at {dll}, but ctypes.CDLL could not load it.') from e
5061
fs_enc = 'utf-8'
62+
5163
else:
5264
import locale
5365
lc, enc = locale.getlocale(locale.LC_NUMERIC)

0 commit comments

Comments
 (0)