本文主要是介绍Ubuntu22.04下源码编译安装pythonocc-7.8,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Ubuntu22.04下源码编译安装pythonocc-7.8
本文介绍Ubuntu下手动编译安装pythonocc,及安装过程遇到的各种坑
基本依赖安装
sudo apt-get update
sudo apt-get install -y wget libglu1-mesa-dev libgl1-mesa-dev libxmu-dev libxi-dev build-essential cmake libfreetype6-dev tk-dev python3-dev rapidjson-dev python3 git python3-pip libpcre2-dev
安装SWIG工具
SWIG是一个用来把C/C++程序和Perl,Python,Ruby,Tcl等脚本语言连接起来的接口编译器。它的工作原理是获取C/C++头文件的声明,并使用它们生成可以访问底层C/C++代码的脚本语言
https://github.com/swig/swig
wget http://prdownloads.sourceforge.net/swig/swig-4.1.1.tar.gz
tar -zxvf swig-4.1.1.tar.gz
cd swig-4.1.1
./configure && make -j4 && make install# 注意安装可能报错没有权限,可以使用超级管理员身份安装`sudo su`
pythonocc核心还是基于C++开发的occ库,python对c++的occ进行包装,如何包装就是使用这个SWIG库
编译构建OCC
下载 occ 7.8.1
下载到本地/home/myPC/occ/occt-bd2a789.tar.gz
cd /home/myPC/occ/
tar -zxvf occt-bd2a789.tar.gzcd occt-bd2a789
mkdir cmake-build
cd cmake-build
开始构建,这里设置一下安装目录/opt/occ781
$ cmake -DINSTALL_DIR=/opt/occt781 -DBUILD_RELEASE_DISABLE_EXCEPTIONS=OFF ..make -j4
sudo make install
将occ库添加到系统依赖库目录
echo "/opt/occt781/lib" >> /etc/ld.so.conf.d/occt.conf
这步可能需要权限,如提示权限问题则
sudo su
执行
开始安装pythonocc-core
首先下载pythonocc源码
git clone https://github.com/tpaviot/pythonocc-core.git
进入目录,开始构建
cd pythonocc-core
mkdir cmake-build && cd cmake-build# 安装目录设置在/opt/pythonocc-core
cmake -DOCCT_INCLbingUDE_DIR=/opt/occt781/include/opencascade -DOCCT_LIBRARY_DIR=/opt/occt781/lib -DPYTHONOCC_BUILD_TYPE=Release -DPYTHONOCC_INSTALL_DIRECTORY=/opt/pythonocc-core ..
执行后报错,发现提示需要SWIG版本不对
-- The C compiler identification is GNU 11.4.0
-- The CXX compiler identification is GNU 11.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found OpenGL: /usr/lib/x86_64-linux-gnu/libOpenGL.so
-- Found Python3: /home/bing/miniconda3/bin/python3.1 (found version "3.11.5") found components: Interpreter Development Development.Module Development.Embed
-- Python3 interpreter:/home/bing/miniconda3/bin/python3.1
-- Python include directory: /home/bing/miniconda3/include/python3.11
-- Python library release: /home/bing/miniconda3/lib/libpython3.11.so
CMake Error at /usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:230 (message):Could NOT find SWIG: Found unsuitable version "4.1.1", but required is atleast "4.2.1" (found /usr/local/bin/swig, )
Call Stack (most recent call first):/usr/share/cmake-3.22/Modules/FindPackageHandleStandardArgs.cmake:592 (_FPHSA_FAILURE_MESSAGE)/usr/share/cmake-3.22/Modules/FindSWIG.cmake:153 (find_package_handle_standard_args)CMakeLists.txt:113 (find_package)
解决办法:要么更新SWIG到4.2.1
,要么尝试下降pythonocc源码对SWIG的依赖;最好选择更新SWIG,需要重新下载编译SWIG源码
这里偷懒,尝试修改一下pythonocc-core/CMakeLists.txt
...
########
# SWIG #
########
find_package(SWIG 4.2.1 REQUIRED)
include(${SWIG_USE_FILE})
...
把4.2.1
改成4.1.1
,重新执行cmake
解决报错(这里不建议这样偷懒,最好还是老老实实安装一个最新的SWIG)
继续安装
make -j4 && make install
添加动态库目录
# 打开 ~/.bashrc 添加如下内容
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/occt781/lib
# 完了后,需要执行一下`source ./bashrc`
验证pythonocc-core是否成功安装
安装完成后,测试验证验证一下
$ python
Python 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from OCC.Core.gp import gp_Pnt
Traceback (most recent call last):File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'OCC'
由上可知,当输入 from OCC.Core.gp import gp_Pnt
提示说OCC
找不到,奇了怪了,查看github官方安装文档
If
PYTHONOCC_INSTALL_DIRECTORY
is unset, it will be installed tosite-packages/OCC
;如果PYTHONOCC_INSTALL_DIRECTORY
未设置,将安装到site-packages/OCC
,这个目录被设置为别的目录了,当时想着这个这类目录统一安装到/opt/pythonocc-core
目录,谁知道这个目录不能乱设置;
site-packages
这个目录三python的第三方依赖包的目录,所以默认情况,就会放在你的python的这个目录下,在python环境下,当import
时,py就能正常找到库;
显然由前面的pythonocc的cmake阶段,被简单理解指定目录了;而一般情况下是不用修改的,自动会识别到python的site-packages目录的,当我们采用的conda,有多个虚拟环境安装时,而我们又采用了手动构建编译pythonocc包时,这是就需要指定这个目录到指定的conda虚拟环境中;
如何查看当前python
的第三方库目录呢,可以采用如下方式查看
# 查看一下当前使用的python
$ which python
/home/myPC/miniconda3/bin/python
# 进入python
$ python
Python 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import site
>>> print(site.getsitepackages())
['/home/myPC/miniconda3/lib/python3.11/site-packages']
现在如何解决安装路径设置错误的办法呢?一种是重新安装pythonocc-core
,不指定安装目录;另外一种是创建个软链接,在当前python
的site-packages
下,创建一个/opt/pythonocc-core
的软链接(前面pythonocc安装时 指定的安装路径)
$ ln -s /opt/pythonocc-core /home/myPC/miniconda3/lib/python3.11/site-packages/OCC
$ python
Python 3.11.5 (main, Sep 11 2023, 13:54:46) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from OCC.Core.gp import gp_Pnt
Traceback (most recent call last):File "<stdin>", line 1, in <module>File "/home/bing/miniconda3/lib/python3.11/site-packages/OCC/Core/gp.py", line 164, in <module>from six import with_metaclass
ModuleNotFoundError: No module named 'six'
又有新的报错,我们安装一个six库
$ conda install six
再次测试pythonocc-occ
$ python
>>> from OCC.Core.gp import gp_Pnt
>>> p = gp_Pnt(1., 2., 3.)
>>> p.X()
1.0
完美跑通python-occ
手动编辑安装
# 下载demo
$ git clone https://github.com/tpaviot/pythonocc-demos
$ cd pythonocc-demos/examples
$ python core_helloworld.py
结束
当然,不想折腾的同学,直接看这里,conda安装occ
这篇关于Ubuntu22.04下源码编译安装pythonocc-7.8的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!