I think this question is also related so I recommend to check it out first:
https://answers.ros.org/question/3198...
When ROS2 builds the src files with colcon, it puts the python files into workspace/install/lib/python3.8/site-packages/<package_name>
. This folder is considered as the root of python packages in the given ROS 2 package.
If you can find the python files there, then you can use the following import statement: import .<file_name>
. Note the .
at the start of the import. You don't have to edit nothing else.
If you want to put the imported files into submodules, then you have to achieve that they show up in the workspace/install/lib/python3.8/site-packages/<package_name>/<submodule_name>
folder. This can be done by replacing the packages
argument in the setup.py
file to the following: packages=["<package_name>", "<packages_name>/<submodule_name>"]
. This way the import statement is import .<submodule_name>.<file_name>
.
Example file structure to import with submodule:
workspace
|-> ...
|-> src
|-> mypackage
|-> mypackage
|-> submodules
|->__init__.py
|-> imported.py
|-> __init__.py
|-> main.py
|-> resource
|-> ...
|-> test
|-> ...
|-> package.xml
|-> setup.cfg
|-> setup.py
setup.py
:
from setuptools import setup
package_name = 'mypackage'
submodules = "mypackage/submodules"
setup(
name=package_name,
version='0.0.0',
packages=[package_name, submodules],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='TODO',
maintainer_email='TODO@email.com',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'main = mypackage.main:main',
],
},
)
main.py
:
from .submodules.imported import function
def main():
function()
if __name__ == '__main__':
main()
imported.py
:
def function():
print("hello world")
What is the path you use for importing that submodule? Do you have an __init__.py?
to import it i said from Submodule.file_that-i-am-trying-to-call.py and yes it has an __init__.py
An interesting thing that happens is when i remove the submodule folder and pull the file in by itself, i can import it like: .file-that-i-am-trying-to-call.py
You shouldn't use dashes in module names, and you shouldn't need the .py extension when importing. I think theoretically it should be import A.Submodule.file_that_i_am_trying_to_call (i.e. with the package name in front). If that doesn't work, please provide a minimal example to reproduce the error.
Hi, So this is for the example i used dashes for the actual implementation i will give you the set of steps to recreate.
I made a python ros2 package just using the customary build instructions I then made a node that includes a submodule for a dstar algorithm implementation.
so the folder (called dstar) contains dstar_algorithm.py and some of its own helper files.
I mispoke when i added a .py at the end of the include it does not have one.
So in my node class, all it does, is it prints hello -- i literally am just testing the ability to pull in different python modules into ros2.
My colcon build passes which is what i expect, but on the run of the node file, it fails on the line that is including the dstar_algorithm .
i import it like from dstar.dstar_algorithm import *
I have also tried: from ...(more)
Have you been able to find a solution? Thanks