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.gz
- cd 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_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版本不对
- -- 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
- ...
- ########
- # SWIG #
- ########
- find_package(SWIG 4.2.1 REQUIRED)
- include(${SWIG_USE_FILE})
- ...
复制代码 把4.2.1改成4.1.1 ,重新执行cmake解决报错(这里不建议这样偷懒,最好照旧老老实实安装一个最新的SWIG)
继续安装
添加动态库目次
- # 打开 ~/.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 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的第三方库目次呢,可以接纳如下方式查看
- # 查看一下当前使用的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库
再次测试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
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |