# ROS compile with python3
###### tags: `ROS` `troubleshooting`
:::info
這篇原本是要自己compile可以跑python3的ros
但過程艱辛
後來發現只要第一次 catkin_make 時:
$ catkin_make -DPYTHON_EXECUTABLE=python3
即可
:::
## 完整版
### 1. 首先安裝基本依賴包:
安裝ros依賴:
```
sudo apt install python3-rosdep2 python3-rosinstall-generator python3-rosinstall build-essential python3-defusedxml python3-netifaces
```
安裝編譯依賴:
```
sudo apt install libeigen3-dev libboost-thread-dev libgtest-dev libconsole-bridge-dev libpoco-dev python3-sip-dev libboost-filesystem-dev libboost-program-options-dev libtinyxml2-dev libtinyxml-dev pyqt5-dev libcurl4-openssl-dev libboost-regex-dev liblz4-dev libbz2-dev libboost-signals-dev liburdfdom-headers-dev liburdfdom-dev uuid-dev libassimp-dev libogre-1.9-dev libyaml-cpp-dev
```
### 2. 配置rosdep
```
mkdir setups
cd setups
touch rosdep
chmod +x ./rosdep
vim ./rosdep
```
裡面貼上:
```
#!/usr/bin/env python3
from rosdep2.main import rosdep_main
rosdep_main()
```
執行:
```
sudo ./rosdep init
./rosdep update
```
如果出現錯誤,就用`rm`將那個檔案刪掉,再執行一次
### 3. 獲取基本ros環境的源碼
首先下載執行文件
```
wget https://raw.githubusercontent.com/ros-infrastructure/rosinstall_generator/master/bin/rosinstall_generator
wget https://raw.githubusercontent.com/vcstools/wstool/master/scripts/wstool
wget https://raw.githubusercontent.com/ros-infrastructure/rospkg/master/scripts/rosversion
wget https://raw.githubusercontent.com/ros-infrastructure/catkin_pkg/master/bin/catkin_find_pkg
wget https://raw.githubusercontent.com/ros-infrastructure/catkin_pkg/master/bin/catkin_create_pkg
wget https://raw.githubusercontent.com/ros-infrastructure/catkin_pkg/master/bin/catkin_generate_changelog
wget https://raw.githubusercontent.com/ros-infrastructure/catkin_pkg/master/bin/catkin_tag_changelog
wget https://raw.githubusercontent.com/ros-infrastructure/catkin_pkg/master/bin/catkin_test_changelog
chmod +x ./rosinstall_generator
chmod +x ./wstool
chmod +x ./rosversion
chmod +x ./catkin_find_pkg
chmod +x ./catkin_create_pkg
chmod +x ./catkin_generate_changelog
chmod +x ./catkin_tag_changelog
chmod +x ./catkin_test_changelog
```
用vim把剛剛所有成功下載的檔案打開,然後第一行改成python3
也就是 `#! /usr/bin/env python3`
再來有兩個檔案沒有下載到:
1. rosinstall_generator
```
sudo apt-get update
sudo apt-get install python-rosinstall-generator
```
2. rosversion
```
sudo apt-get install python3-rospkg
```
新建總工程目錄並執行ros源代碼下載
```
mkdir ../workspace
cd ../workspace
rosinstall_generator desktop_full --rosdistro melodic --deps --tar > melodic-desktop-full.rosinstall
../setups/wstool init -j8 src melodic-desktop-full.rosinstall
```
---
#### 錯誤
`
ERROR Cannot find rosinstall libraries, check your installation. One frequent cause is that rosinstall 0.5 is still installed in /usr/local/lib. No module named rosws_cli
`
#### 解決
先更新
```
sudo apt-get update python-rosinstall python-vcstools
```
失敗了再把之前的remove 可以檢查有沒有,如果沒有的話,直接裝新的
```
sudo rm -rf /usr/local/lib/python2.7/dist-packages/rosinstall*
sudo rm -rf /usr/local/lib/python2.7/dist-packages/vcstools*
sudo rm -rf /usr/local/bin/rosws /usr/local/bin/roslocate /usr/local/bin/rosinstall /usr/local/bin/rosco
```
直接裝新的
```
sudo apt-get install python-rosinstall python-vcstools
```
如果安裝失敗的話,可以試試繼續安裝:
```
../setups/wstool update -j 8 -t src
```
---
接下來解決依賴
```
../setups/rosdep install --from-paths src --ignore-src --rosdistro melodic -y
```
### 4. 安裝Catkin
指令為
```
sudo apt-get install git
git clone https://github.com/ros/catkin
sudo apt-get install cmake python-catkin-pkg python-empy python-nose python-setuptools libgtest-dev build-essential
cd catkin/ && mkdir build && cd build && cmake -DCMAKE_BUILD_TYPE=Release ../ && make && sudo make install
```
```
export ROS_PYTHON_VERSION=3
cd ~/workspace/catkin/build
cmake ..
make
sudo make install
```
然後回到workspace目錄
- 修改 `./src/orocos_kinematics_dynamics/python_orocos_kdl/CMakeLists.txt`中
`SET(PYTHON_VERSION 2 CACHE STRING "Python Version")`
改為
`SET(PYTHON_VERSION 3 CACHE STRING "Python Version")`
- 修改 `src/dynamic_reconfigure/cfg/Test.cfg` 和 `src/nodelet_core/nodelet_topic_tools/cfg/NodeletThrottle.cfg` 第一行爲
`
#! /usr/bin/env python3
`
- 添加 regex 依賴到 `src/rosconsole/CMakeLists.txt` 72 行後(維持其他不變,添加boost_regex)
```
target_link_libraries(rosconsole ${rosconsole_backend_LIBRARIES} ${catkin_LIBRARIES} ${Boost_LIBRARIES} boost_regex)
```
- 修改 `src/vision_opencv/cv_bridge/CMakeLists.txt` 11 行爲
`
find_package(Boost REQUIRED python-py36)
`
- `~/.bashrc` 最後一行添加
`
export ROS_PYTHON_VERSION=3
`
### 5. 開始編譯
```
source ~/.bashrc
catkin_make_isolated --install -DCMAKE_BUILD_TYPE=Release
```
### 6. 開始使用ROS
執行可以同時加入 `~/.bashrc`
```
source ~/workspace/install_isolated/setup.bash
```
再來就
```
roscore
rosrun
```