ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
Hi there, I found a solution here : https://roboticsbackend.com/ros-import-python-module-from-another-package/
Tried it, and it worked perfectly.
2 | No.2 Revision |
Hi there, I found a solution here : https://roboticsbackend.com/ros-import-python-module-from-another-package/
edit :
As mentioned in the blog : Step 1: create a setup.py file inside my Package (myPkg) folder and posted this code :
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup
d = generate_distutils_setup(
packages=['myPkg'],
package_dir={'': 'src'}
)
setup(**d)
and just save the file without executing it.
Step 2: Create a Folder inside ./myPkg/src/ with the same name as the pakage (myPkg).
Step 3: Go to ./myPkg/src/myPkg/ and Create a new file __init__.py and save it as an empty file.
Step 4: In the same folder, Create and save the python module files (myModule.py) you want to import in your node later. Note : Make sure to import rospy
in that module
Step 5: Go back to the package folder ./myPkg/ , open CMakeLists.txt
and uncomment the line catkin_python_setup()
and save the file.
Step 6: Go back to your catkin workspace folder where myPkg package is, say cd ~/catkin_ws/
and do catkin_make
.
Now, if I create a python node in ./myPkg/src/ , I can import myModule.py using the line from myPkg import myModule
.
Tried it, and it worked perfectly.
Hope it helps.