Comment Comment by slowking2 You can use tempfiles and (on OSX) use the private API of ctypes to dlclose to close the handle. Different call on Windows I think. Something like import _ctypes import shutil import tempfile @pytest.fixture def libfact(): tmp = tempfile.NamedTemporaryFile(delete=True) shutil.copy2("./fact.so", tmp.name) lib = CDLL(tmp.name) yield lib _ctypes.dlclose(lib.handle) EDIT: fixed error mentioned in reply. parent slowking2 4 years ago