Python Forum

Full Version: How To Unload Windows Native Extension ( .pyd )?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am looking for a way to get the Python interpreter to release a native Python extension file ( .pyd ) so that I can delete the file when running on Windows.

I have a native Python extension, written in Rust, that is named "core" and is compiled to a windows DLL with a .pyd extension creating the file core.pyd. I put this core.pyd file in the root of my Python module which is named arsenal-blender.

In my arsenal-blender python module I import the native library like so:

from . import core
To be clear, this is all working. I am able to execute the code from core module and it works fine.

My problem is that, after importing the core module, I need a way to tell Python to "unload" the module so that Windows will allow me to delete the core.pyd file.

The reason I need to be able to do this is because arsenal-blender is a Blender plugin and Blender will try to delete the arsenal-blender folder and everything in it when the plugin is uninstalled. The issue is that Blender, and its Python instance, is still running and the core.pyd file is still open in the Python interpreter, so Windows will not allow me to delete the file.

Is there any way for me to get the Python interpreter to release that file so that it can be deleted?

This is only a problem on Windows because Unix is able to simply "unlink" the file while Python is still using it.
(Jan-01-2020, 02:12 AM)zicklag Wrote: [ -> ]I am looking for a way to get the Python interpreter to release a native Python extension file ( .pyd ) so that I can delete the file when running on Windows.

I have a native Python extension, written in Rust, that is named "core" and is compiled to a windows DLL with a .pyd extension creating the file core.pyd. I put this core.pyd file in the root of my Python module which is named arsenal-blender.

In my arsenal-blender python module I import the native library like so:

from . import core
To be clear, this is all working. I am able to execute the code from core module and it works fine.

My problem is that, after importing the core module, I need a way to tell Python to "unload" the module so that Windows will allow me to delete the core.pyd file.

The reason I need to be able to do this is because arsenal-blender is a Blender plugin and Blender will try to delete the arsenal-blender folder and everything in it when the plugin is uninstalled. The issue is that Blender, and its Python instance, is still running and the core.pyd file is still open in the Python interpreter, so Windows will not allow me to delete the file.

Is there any way for me to get the Python interpreter to release that file so that it can be deleted?

This is only a problem on Windows because Unix is able to simply "unlink" the file while Python is still using it.

Did you ever find a solution to this? Having the same problem
Why are you unloading a module and want to delete the directory/file?
This would be the question of the Author to understand your requirement.
Maybe you can open an issue on github: https://github.com/katharostech/arsenal
Like the original poster, I have written a Blender addon which requires a dll (.pyd for Windows). When 'Remove' is clicked within Blender to remove this addon, a function is called which deletes the files from their path in Blender's AppData. The issue is that if the dll is not unloaded before this remove process is attempted, os.unlink cannot act on the dll as access is denied. The addon I am developing is a commercial addon and so not being able to remove it after installation impacts on customer experience. I have been looking for a way to unload the dll using python for a while now but have had no luck.