Skip to content

Commit 6fe68a6

Browse files
authored
Merge pull request #7 from psavery/python-site-packages
Ensure tomviz built-in Python libraries get installed correctly
2 parents c3c8392 + eeedc88 commit 6fe68a6

3 files changed

Lines changed: 63 additions & 22 deletions

File tree

CMakeLists.txt

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,18 @@ if(UNIX AND NOT APPLE)
3232
endif()
3333
endif()
3434

35+
find_package(Python3 COMPONENTS Interpreter Development)
36+
37+
# Just add these to the cache so we can check them more easily...
38+
set(PYTHON_INCLUDE_DIR ${Python3_INCLUDE_DIR} CACHE PATH "Tomviz")
39+
set(PYTHON_LIBRARY ${Python3_LIBRARY} CACHE PATH "Tomviz")
40+
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE} CACHE PATH "Tomviz")
41+
42+
# This will be used by tomvizPythonConfig.h.in
43+
add_definitions(-DPYTHON_VERSION_MINOR="${Python3_VERSION_MINOR}")
44+
3545
# Location where Python modules will be installed.
36-
set(tomviz_python_install_dir "${INSTALL_LIBRARY_DIR}/tomviz/site-packages")
46+
set(tomviz_python_install_dir "${INSTALL_LIBRARY_DIR}/python3.${Python3_VERSION_MINOR}/site-packages")
3747
# Location where Python modules will be copied to in binary tree.
3848
set(tomviz_python_binary_dir "${tomviz_BINARY_DIR}/lib/site-packages")
3949
# Location where sample data will be installed.
@@ -55,12 +65,14 @@ if(APPLE)
5565
set(INSTALL_DATA_DIR "${prefix}/${INSTALL_DATA_DIR}")
5666
set(INSTALL_DOC_DIR "${prefix}/${INSTALL_DOC_DIR}")
5767
set(INSTALL_CMAKE_DIR "${prefix}/Resources")
58-
set(tomviz_python_install_dir "${prefix}/Python")
5968
set(tomviz_data_install_dir "${prefix}/share/tomviz")
6069
set(tomviz_web_install_dir "${prefix}/share/tomviz/web")
6170
if(NOT CMAKE_INSTALL_NAME_DIR)
6271
set(CMAKE_INSTALL_NAME_DIR "${CMAKE_INSTALL_PREFIX}/${INSTALL_LIBRARY_DIR}")
6372
endif()
73+
elseif(WIN32)
74+
# Windows is a little different...
75+
set(tomviz_python_install_dir "lib/site-packages")
6476
endif()
6577

6678
# Get the relative path from the tomviz executable install location
@@ -134,17 +146,6 @@ endif()
134146

135147
find_package(ITK 4.9)
136148

137-
# Many things changed in the find Python logic and ParaView's willingness to
138-
# share what version of Python it built against. In the end it wasn't worth
139-
# trying to support doing this automatically - try to feed everything the same
140-
# version of Python, using a superbuild will make this easier for you.
141-
find_package(Python3 COMPONENTS Interpreter Development)
142-
143-
# Just add these to the cache so we can check them more easily...
144-
set(PYTHON_INCLUDE_DIR ${Python3_INCLUDE_DIR} CACHE PATH "Tomviz")
145-
set(PYTHON_LIBRARY ${Python3_LIBRARY} CACHE PATH "Tomviz")
146-
set(PYTHON_EXECUTABLE ${Python3_EXECUTABLE} CACHE PATH "Tomviz")
147-
148149
set(PYBIND11_CPP_STANDARD "-std=c++11" CACHE STRING "")
149150
add_subdirectory(${PROJECT_SOURCE_DIR}/thirdparty/pybind11)
150151

tomviz/python/tomviz/state/_schemata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,11 @@ def remove_empty(self, data, **kwargs):
212212

213213

214214
class DataSourceSchema(Schema):
215-
operators = fields.List(OperatorField, missing=lambda: [])
215+
operators = fields.List(OperatorField)
216216
colorMap2DBox = fields.Nested(ColorMap2DBoxSchema)
217217
colorOpacityMap = fields.Nested(ColorOpacityMap)
218218
gradientOpacityMap = fields.Nested(GradientOpacityMap)
219-
modules = fields.List(ModuleField, missing=lambda: [])
219+
modules = fields.List(ModuleField)
220220
useDetachedColorMap = fields.Boolean()
221221
spacing = fields.List(fields.Float)
222222
units = fields.String()

tomviz/tomvizPythonConfig.h.in

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818
#define TOMVIZ_DATA_INSTALL_DIR "@tomviz_data_install_dir@/Data"
1919
#define TOMVIZ_PATH_FROM_EXE_TO_PYTHON_INSTALL_DIR "@tomviz_path_from_exe_to_python_install_dir@"
2020

21+
#include <cstdlib>
2122
#include <string>
2223
#include <vtksys/SystemTools.hxx>
2324

2425
#include "PythonUtilities.h"
2526

27+
// Used for determining paths to the site packages
28+
static const std::string PYTHON_VER = "3." PYTHON_VERSION_MINOR;
29+
2630
namespace tomviz {
2731
void PythonAppInitPrependPythonPath(const std::string& dir)
2832
{
@@ -62,6 +66,16 @@ void PythonAppInitPrependPathWindows(const std::string& exe_dir)
6266
PythonAppInitPrependPythonPath(exe_dir +
6367
"/../lib/paraview/site-packages/vtk");
6468
PythonAppInitPrependPythonPath(exe_dir + "/../lib/itk/site-packages");
69+
PythonAppInitPrependPythonPath(exe_dir +
70+
"/../lib/python" +
71+
PYTHON_VER +
72+
"/site-packages");
73+
}
74+
75+
// Prepend conda prefix site packages if available
76+
std::string conda_prefix = std::getenv("CONDA_PREFIX");
77+
if (!conda_prefix.empty()) {
78+
PythonAppInitPrependPythonPath(conda_prefix + "/Lib/site-packages");
6579
}
6680
}
6781

@@ -87,14 +101,27 @@ void PythonAppInitPrependPathLinux(const std::string& exe_dir)
87101
// since exe_dir in <PREFiX>/bin, we do a /../
88102
PythonAppInitPrependPythonPath(exe_dir + "/../" TOMVIZ_PYTHON_INSTALL_DIR);
89103

90-
// Add the location for numpy and scipy
91-
PythonAppInitPrependPythonPath(exe_dir + "/../lib/python3.7/site-packages");
104+
auto python_path = exe_dir + "/../lib/python" + PYTHON_VER;
92105

93-
// Add root python3.7 dir
94-
PythonAppInitPrependPythonPath(exe_dir + "/../lib/python3.7");
106+
// Add root python dir
107+
PythonAppInitPrependPythonPath(python_path);
108+
109+
// Add the location for numpy and scipy
110+
PythonAppInitPrependPythonPath(python_path + "/site-packages");
95111

96112
// Add the location of python .so libraries
97-
PythonAppInitPrependPythonPath(exe_dir + "/../lib/python3.7/lib-dynload");
113+
PythonAppInitPrependPythonPath(python_path + "/lib-dynload");
114+
}
115+
116+
// Prepend conda prefix site packages if available
117+
std::string conda_prefix = std::getenv("CONDA_PREFIX");
118+
if (!conda_prefix.empty()) {
119+
PythonAppInitPrependPythonPath(
120+
conda_prefix +
121+
"/lib/python" +
122+
PYTHON_VER +
123+
"/site-packages"
124+
);
98125
}
99126
}
100127

@@ -119,11 +146,24 @@ void PythonAppInitPrependPathOsX(const std::string& exe_dir)
119146
// we don't add anything for ParaView since, ParaView takes care of that.
120147
PythonAppInitPrependPythonPath(exe_dir + "/../Libraries/");
121148
PythonAppInitPrependPythonPath(exe_dir +
122-
"/../Libraries/python3.7/site-packages");
149+
"/../Libraries/python" +
150+
PYTHON_VER +
151+
"/site-packages");
123152
PythonAppInitPrependPythonPath(exe_dir + "/../Python");
124153
PythonAppInitPrependPythonPath(exe_dir + "/../Python/lib-dynload");
125154
PythonAppInitPrependPythonPath(exe_dir + "/../Python/site-packages");
126155
}
156+
157+
// Prepend conda prefix site packages if available
158+
std::string conda_prefix = std::getenv("CONDA_PREFIX");
159+
if (!conda_prefix.empty()) {
160+
PythonAppInitPrependPythonPath(
161+
conda_prefix +
162+
"/lib/python" +
163+
PYTHON_VER +
164+
"/site-packages"
165+
);
166+
}
127167
}
128168

129169
bool isApplicationBundle(const std::string& exeDir)
@@ -147,7 +187,7 @@ std::string bundlePythonPath(const std::string& exeDir)
147187
#elif defined(_WIN32)
148188
return "";
149189
#else
150-
return exeDir + "/../lib/python3.7";
190+
return exeDir + "/../lib/python" + PYTHON_VER;
151191
#endif
152192
}
153193

0 commit comments

Comments
 (0)