我想从源代码构建 SimpleITK 并为 Python 3.4(在 Ubuntu 14.04 上)安装它。
我关注了SimpleITK's build instructions ,即
git clone http://itk.org/SimpleITK.git
mkdir SimpleITK-build
cd SimpleITK-build
cmake ../SimpleITK/SuperBuild
而且效果很好。我能够使用 sudo python setup.py install
为 Python 2.7 安装 SimpleITK。然后我尝试使用 sudo python3.4 setup.py install
为 Python 3.4 安装它,虽然它似乎安装正确,但当我尝试在 Python 3 中导入 SimpleITK 时,我得到:
...
File "/usr/lib/python3.4/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
ImportError: dynamic module does not define init function (PyInit__SimpleITK)
在上面的构建说明中,它说:
Verify and/or correct the advanced cmake variables to the language specific executable, libraries and include directories. For example if you have multiple Python installations ensure that all related Python variable refer to the same versions.
我没有这样做,我怀疑这就是它只适用于 Python 2.7 的原因。如何更正“高级 cmake 变量”?
最佳答案
以下是我在 Linux 上使用的所有步骤(构建 Yiuin 的答案和 official docs)。
获取SimpleITK源码
cd ~
git clone --recursive http://itk.org/SimpleITK.git
设置最新的稳定版本
cd ~/SimpleITK
git tag
寻找最新的稳定版本(对我来说是“v0.8.0”)并通过执行检查
git checkout v0.8.0
不要担心有关“分离的 HEAD”的警告。
构建 SimpleITK
cd ~
mkdir SimpleITK-build
cd SimpleITK-build
这里我使用了 CMake GUI(在 Ubuntu 上,使用 sudo apt-get install cmake-qt-gui
),
cmake-gui ../SimpleITK/SuperBuild/
这将打开一个 GUI,您可以从中编辑 cmake 变量。单击按钮 Configure
并在弹出的对话框中单击 Finish
,然后启用 Advanced
。更改所有相关的 Python 变量:
(我不确定为什么有些路径末尾有“m”,但您可以阅读更多相关信息 here )
如果您想关闭 Java 或任何其他语言,请向下滚动并禁用 WRAP_JAVA
。再次点击 Configure
(不确定是否有必要),然后点击 Generate
。关闭 CMake GUI,返回您的终端并输入
make
这至少需要一个小时。
现在将此行添加到您的 .bashrc 文件
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:~/SimpleITK-build/lib
在 Python 上安装
我使用 checkinstall
来干净地管理安装,
cd ~
sudo checkinstall --pkgname python3-simpleitk python3.4 \
~/SimpleITK-build/SimpleITK-build/Wrapping/PythonPackage/setup.py install
关于python-3.x - 使用 CMake 为 Python 3 构建 SimpleITK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24219159/