Skip to content

Commit

Permalink
On Windows, use os.add_dll_directory to point to location of ThirdP…
Browse files Browse the repository at this point in the history
…arty dlls. (#19258) (#19259)

To fix issues where Python 3.9 cannot import vtk modules due to change in how paths are searched for DLLs.
Added function to retrieve ThirdParty path for dev version of Windows (for non-dev, the dlls live with the executables).
Use that path in call to  `os.add_dll_directory`.
  • Loading branch information
biagas committed Jan 26, 2024
1 parent 8d29075 commit 8f22a45
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/avt/PythonFilters/PythonInterpreter.C
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ PythonInterpreter::AddSystemPath(const std::string &path)
return RunScript("sys.path.insert(1,r'" + path + "')\n");
}

#ifdef _WIN32
// ****************************************************************************
// Method: PythonInterpreter::AddDLLPath
//
// Purpose:
// Adds passed path to "os.add_dll_directory"
//
// Arguments:
// path Path to DLLs (eg VisIt's third party dlls)
//
// Programmer: Kathleen Biagas
// Creation: January 24, 2024
//
// Modifications:
//
// ****************************************************************************

bool
PythonInterpreter::AddDLLPath(const std::string &path)
{
return RunScript("os.add_dll_directory(r'" + path + "')\n");
}
#endif

// ****************************************************************************
// Method: PythonInterpreter::RunScript
//
Expand Down
7 changes: 7 additions & 0 deletions src/avt/PythonFilters/PythonInterpreter.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ typedef _object PyObject;
// Programmer: Cyrus Harrison
// Creation: May 2, 2008
//
// Modifications:
// Kathleen Biagas, Wed Jan 24, 2024
// Added new method 'AddDLLPath'.
//
// ****************************************************************************
class AVTPYTHON_FILTERS_API PythonInterpreter
{
Expand All @@ -36,6 +40,9 @@ class AVTPYTHON_FILTERS_API PythonInterpreter
void Shutdown();

bool AddSystemPath(const std::string &path);
#ifdef _WIN32
bool AddDLLPath(const std::string &path);
#endif
bool RunScript(const std::string &script);
bool RunScriptFile(const std::string &fname);

Expand Down
14 changes: 14 additions & 0 deletions src/avt/PythonFilters/avtPythonFilterEnvironment.C
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ avtPythonFilterEnvironment::~avtPythonFilterEnvironment()
// Cyrus Harrison, Tue Feb 23 17:04:32 PST 2021
// Use MPI_Comm_c2f handle to init mpicom module.
//
// Kathleen Biagas, Wed Jan 24, 2024
// When running a dev version on Windows, add ThirdParty directory
// to dll directory.
//
// ****************************************************************************

bool
Expand All @@ -113,6 +117,16 @@ avtPythonFilterEnvironment::Initialize()
if(!pyi->AddSystemPath(vlibsp)) // vtk module is symlinked here
return false;

#ifdef _WIN32
// need to add ThirdParty dll directory to dll path if it is available
// (eg for development builds)
if(GetIsDevelopmentVersion())
{
string vdlldir = GetVisItThirdPartyDirectory();
if(!vdlldir.empty() && !pyi->AddDLLPath(vdlldir))
return false;
}
#endif
// import pyavt and vtk
if(!pyi->RunScript("from pyavt.filters import *\n"))
return false;
Expand Down
34 changes: 34 additions & 0 deletions src/common/misc/InstallationFunctions.C
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,40 @@ GetSystemVisItHostsDirectory()
return retVal;
}

#ifdef _WIN32
#include <filesystem>
// ****************************************************************************
// Method: GetVisItThirdPartyDirectory
//
// Purpose:
// Returns the path to the ThirdParty directory for a dev build of VisIt.
// Returns an empty string if not running from a development build.
//
// Arguments:
// none
//
// Programmer: Kathleen Biagas
// Creation: January 24, 2024
//
// Modifications:
//
// ****************************************************************************

std::string
GetVisItThirdPartyDirectory()
{
std::string retval;
if(GetIsDevelopmentVersion())
{
std::filesystem::path homeDir(GetVisItInstallationDirectory());
auto dllDir = homeDir.parent_path() / "ThirdParty";
retval = dllDir.string();
}
return retval;
}

#endif

// ****************************************************************************
// Method: GetVisItResourcesDirectory
//
Expand Down
4 changes: 4 additions & 0 deletions src/common/misc/InstallationFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ std::string MISC_API GetAndMakeUserVisItHostsDirectory();
std::string MISC_API GetVisItHostsDirectory();
std::string MISC_API GetSystemVisItHostsDirectory();

#ifdef _WIN32
std::string MISC_API GetVisItThirdPartyDirectory();
#endif

typedef enum {
VISIT_RESOURCES,
VISIT_RESOURCES_COLORTABLES,
Expand Down
12 changes: 12 additions & 0 deletions src/visitpy/cli/cli.C
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,18 @@ main(int argc, char *argv[])
oss << "sys.path.append(pjoin(r'" << vlibdir <<"','site-packages'))";
PyRun_SimpleString(oss.str().c_str());

#ifdef _WIN32
if(GetIsDevelopmentVersion())
{
// retrieve ThirdParty directory
std::string dlldir = GetVisItThirdPartyDirectory();
// Add thirdparty DLL's directory
std::ostringstream ss;
ss << "os.add_dll_directory(r'" << dlldir <<"')";
PyRun_SimpleString(ss.str().c_str());
}
#endif

// Initialize the VisIt module.
cli_initvisit(bufferDebug ? -debugLevel : debugLevel,
verbose,
Expand Down

0 comments on commit 8f22a45

Please sign in to comment.