Ubuntu22.04下源码编译安装pythonocc-7.8

Ubuntu22.04下源码编译安装pythonocc-7.8

本文介绍Ubuntu下手动编译安装pythonocc,及安装过程遇到的各种坑

基本依赖安装

bash 复制代码
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

bash 复制代码
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

bash 复制代码
cd /home/myPC/occ/
tar -zxvf occt-bd2a789.tar.gz

cd occt-bd2a789
mkdir cmake-build
cd cmake-build

开始构建,这里设置一下安装目录/opt/occ781

bash 复制代码
$ cmake -DINSTALL_DIR=/opt/occt781 -DBUILD_RELEASE_DISABLE_EXCEPTIONS=OFF ..

make -j4
sudo make install

将occ库添加到系统依赖库目录

bash 复制代码
echo "/opt/occt781/lib" >> /etc/ld.so.conf.d/occt.conf

这步可能需要权限,如提示权限问题则sudo su执行

开始安装pythonocc-core

首先下载pythonocc源码

bash 复制代码
git clone https://github.com/tpaviot/pythonocc-core.git

进入目录,开始构建

bash 复制代码
cd pythonocc-core
mkdir cmake-build && cd cmake-build

# 安装目录设置在/opt/pythonocc-core

cmake  -DOCCT_INCLUDE_DIR=/opt/occt781/include/opencascade  -DOCCT_LIBRARY_DIR=/opt/occt781/lib  -DPYTHONOCC_BUILD_TYPE=Release -DPYTHONOCC_INSTALL_DIRECTORY=/opt/pythonocc-core ..

# 如直接采用目录python路径,可不设置目录参数 -DPYTHONOCC_INSTALL_DIRECTORY
# cmake  -DOCCT_INCLUDE_DIR=/opt/occt781/include/opencascade  -DOCCT_LIBRARY_DIR=/opt/occt781/lib  -DPYTHONOCC_BUILD_TYPE=Release ..
# 安装时,则会安装到默认的python第三方包目录

执行后报错,发现提示需要SWIG版本不对

bash 复制代码
-- 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 at
  least "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

CmakeLists 复制代码
...
########
# SWIG #
########
find_package(SWIG 4.2.1 REQUIRED)
include(${SWIG_USE_FILE})
...

4.2.1改成4.1.1 ,重新执行cmake解决报错(这里不建议这样偷懒,最好还是老老实实安装一个最新的SWIG)

继续安装

bash 复制代码
make -j4 && make install 

添加动态库目录

bash 复制代码
# 打开 ~/.bashrc 添加如下内容
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/occt781/lib
# 完了后,需要执行一下`source ./bashrc`

验证pythonocc-core是否成功安装

安装完成后,测试验证验证一下

bash 复制代码
$ 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 to site-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的第三方库目录呢,可以采用如下方式查看

bash 复制代码
# 查看一下当前使用的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,不指定安装目录;另外一种是创建个软链接,在当前pythonsite-packages下,创建一个/opt/pythonocc-core的软链接(前面pythonocc安装时 指定的安装路径)

bash 复制代码
$ ln -s /opt/pythonocc-core /home/myPC/miniconda3/lib/python3.11/site-packages/OCC
bash 复制代码
$ 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库

bash 复制代码
$ conda install six

再次测试pythonocc-occ

bash 复制代码
$ python
>>> from OCC.Core.gp import gp_Pnt
>>> p = gp_Pnt(1., 2., 3.)
>>> p.X()
1.0

完美跑通python-occ手动编辑安装

bash 复制代码
# 下载demo
$ git clone https://github.com/tpaviot/pythonocc-demos
$ cd pythonocc-demos/examples  
$ python core_helloworld.py

结束

当然,不想折腾的同学,直接看这里,conda安装occ

相关推荐
一点灯2 个月前
opencascade源码学习之HLRAlgo包 -HLRAlgo
occ·opencascade
CodingCos3 个月前
【SOC 芯片设计 DFT 学习专栏 -- DFT OCC 与 ATPG的介绍】
occ·atpg·fast scan·dft occ
VinciYan4 个月前
编译Open Cascade(OCC)并使用C#进行开发
c#·开源软件·cad·occ·open cascade·三维cad
Hanson855 个月前
OCC介绍及框架分析
occ·cad平台·几何建模内核
wuchen10046 个月前
OpenCASCADE(OCC)读入含中文汉字标签的内容后乱码的解决方法
occ·opencascade·标签乱码
Imkc7 个月前
OOCT WPF_D3D项目报错无法加载依赖项
occt
欧特克_Glodon8 个月前
OpenCASCADE开发指南:专栏内容介绍及目录
c++·三维建模·occ·opencascade·几何内核
shanql1 年前
OCC:第一个程序,对话框中显示一个BOX
occ·occ第一个程序