Reference article:
PyCharm installs PyKDL separately
How to compile PyKDL from scratch in a conda virtual environment based on python3
1. Problem description
import PyKDL
ModuleNotFoundError: No module named 'PyKDL'
This is caused by the fact that PyKDL does not exist in the environment used. In fact, PyKDL is installed together when ROS is installed, but its path is in the system environment by default. In my case, the actual location is at
/lib/python3/dist-packages
Because the default python version of my local ubuntu is Python3.8
, it is compiled PyKDL
as PyKDL.cpython-38-x86_64-linux-gnu.so
but in actual demand, PyKDL is generally not run in the default environment. At this time, it needs to be recompiled. Python3.9 in the conda environment I use is example
2. Download the source code
Open the terminal under /home
ie 主目录
, then enter the PyKDL source code address and then git down
git clone https://github.com/orocos/orocos_kinematics_dynamics.git
3. Source code compilation
After completing the above operations, you can start compiling,
first complete the compilation of the C language file, and then compile the Python file
3.1 C文件编译
Terminal cd /orocos_kinematics_dynamics/orocos_kdl
, then enter the following command:
mkdir build
cd build
cmake ../
make
sudo make install
3.2 Python文件编译
Terminal cd /orocos_kinematics_dynamics/python_orocos_kdl
, then enter the following command:
mkdir build
cd build
cmake ../
make
sudo make install
The cmake step can also specify the environment location, taking mine as an examplecmake ../ -DPYTHON_EXECUTABLE:FILEPATH=/home/mango/anaconda3/bin/python3.9
But in the cmake step, the following error may occur
解决方法:
Open the terminal and enter the following command
cd /orocos_kinematics_dynamics/python_orocos_kdl
git submodule update --init
This will recompile and install pybind11.
4. Copy the PyKDL.so file to the specified environment
After the compilation is complete, copy it /orocos_kinematics_dynamics/python_orocos_kdl/build/devel/lib/python3/dist-packages/PyKDL.so
to your conda environment site-packages folder,
The path on my side is/home/mango/anaconda3/lib/python3.9/site-packages
At this time, open the python of the terminal and run the following code to test it. If it can run normally, it proves successful.
import PyKDL
data = PyKDL.Vector(1, 2, 3)
print(data)
If import PyKDL
you still get an error,
edit the .bashrc file in the home directory, that is gedit .bashrc
, add the path
export PYTHONPATH="/home/mango/anaconda3/lib/python3.9/site-packages:$PYTHONPATH"
But doing so may cause errors to be reported when running other programs. If an error is reported, then consider commenting out this path.