Skip to content

Commit 37ec938

Browse files
omichelCopilotCopilot
authored
Fix python controllers on Windows (cyberbotics#6933)
* Fix python controllers on Windows * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Support from Python 3.8 only * simplified * Fix DLL directory handle retention in Python controller on Windows (cyberbotics#6934) * Initial plan * Store DLL directory handles to prevent garbage collection Co-authored-by: omichel <1264964+omichel@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: omichel <1264964+omichel@users.noreply.github.com> * Update changelog for R2025 with Python fix Added entry for fixing Python controllers on Windows. --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: omichel <1264964+omichel@users.noreply.github.com>
1 parent 23d9d8b commit 37ec938

4 files changed

Lines changed: 25 additions & 2 deletions

File tree

docs/guide/preferences.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Note that this is the maximum number of threads allowed, but the actual number o
2222
- The **Python command** defines which Python command is invoked by Webots when starting a Python controller.
2323
The default value is `python`.
2424
It should work on most systems assuming that `python` is installed and available from the command line.
25-
On some systems, it may be useful to set it to `python3.7` for example if you want to launch the controllers with this specific version of Python.
25+
On some systems, it may be useful to set it to `python3.14` for example if you want to launch the controllers with this specific version of Python.
2626
Bear in mind that this value may be overridden by the content of a `runtime.ini` file of a Python controller that may redefine a specific Python command to launch that controller.
2727
- The **Extra project path** defines the paths to user folders similar to the `WEBOTS_HOME/projects` folder.
2828
These user folders should contain projects resources that can be used in the current project (such as PROTO nodes, controllers, textures, etc.).

docs/guide/using-python.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The Python API is currently composed of a set of about 25 classes having about 2
88
The classes are either representations of a node of the scene tree (such as Robot, LED, etc.) or utility classes (such as Motion, ImageRef, etc.).
99
A complete description of these functions can be found in the reference guide while the instructions about the common way to program a Python controller can be found in [this chapter](programming-fundamentals.md).
1010

11-
The Python API of Webots supports Python versions from 3.7.
11+
The Python API of Webots supports Python versions from 3.8.
1212

1313
Alternatively to the Webots built-in editor, [PyCharm](https://www.jetbrains.com/pycharm) can be used to edit and launch Python controllers, see the [Using PyCharm with Webots](using-your-ide.md#pycharm) chapter for a step-by-step procedure.
1414

docs/reference/changelog-r2025.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- Fixed a crash occurring when Python was not found on Windows ([#6870](https://github.com/cyberbotics/webots/pull/6870)).
2323
- Fixed detection of physics plugins in extra projects ([#6880](https://github.com/cyberbotics/webots/pull/6880)).
2424
- Fixed `addForceWithOffset` and `addTorque` doing the same thing as `addForce` in Python ([#6881](https://github.com/cyberbotics/webots/pull/6881)).
25+
- Fixed Python controllers on Windows ([#6933](https://github.com/cyberbotics/webots/pull/6933)).
2526

2627
## Webots R2025a
2728
Released on January 31st, 2025.

lib/controller/python/controller/wb.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,32 @@
1616
import os
1717
import sys
1818

19+
# Keep DLL directory handles alive to prevent garbage collection (Windows only)
20+
_dll_directory_handles = []
21+
1922
if sys.platform == 'linux' or sys.platform == 'linux2':
2023
path = os.path.join('lib', 'controller', 'libController.so')
2124
elif sys.platform == 'win32':
2225
path = os.path.join('lib', 'controller', 'Controller.dll')
26+
# Since Python 3.8, dependent DLLs are no longer found via PATH on Windows.
27+
# Add directories containing required runtime DLLs to the search path.
28+
webots_home = os.environ['WEBOTS_HOME']
29+
for dll_dir in [
30+
os.path.join(webots_home, 'lib', 'controller'),
31+
# In the installed distribution, MinGW runtime DLLs (libgcc_s_seh-1.dll,
32+
# libwinpthread-1.dll, libstdc++-6.dll) are placed in msys64/mingw64/bin/cpp.
33+
os.path.join(webots_home, 'msys64', 'mingw64', 'bin', 'cpp'),
34+
os.path.join(webots_home, 'msys64', 'mingw64', 'bin'),
35+
]:
36+
if os.path.isdir(dll_dir):
37+
_dll_directory_handles.append(os.add_dll_directory(dll_dir))
38+
# For development builds running from source, also check the system MSYS2 installation.
39+
mingw_bin = os.path.join(os.environ.get('MSYS2_HOME',
40+
os.path.splitdrive(sys.executable)[0] + os.sep + 'msys64'),
41+
'mingw64',
42+
'bin')
43+
if os.path.isdir(mingw_bin):
44+
_dll_directory_handles.append(os.add_dll_directory(mingw_bin))
2345
elif sys.platform == 'darwin':
2446
path = os.path.join('Contents', 'lib', 'controller', 'libController.dylib')
2547

0 commit comments

Comments
 (0)