Python file updates not seen after building and sourcing

Hello,
I’m doing the project for ros2 in five days, and I changed a line in my python file. However, after building and sourcing. The error persisits, it’s as I didn’t change the code. attached screenshots. As you can see the line is importing the service.
Screenshot 2024-05-14 091635



Hi @BushraAlsh ,

This is a commonly seen compilation issue with ROS2.
If you think that changes have not been persisted after a recent compilation, then you need to compile from scratch.
You simply remove the build, install and log folders and compile your workspace.

cd ~/ros2_ws
rm -rf ./build ./install ./log
colcon build   # compiles all packages
colcon build --packages-select <package_name>   # compiles specified packages
source install/setup.bash
ros2 launch <package_name> <launch_file_name>

This should fix your problem. Let me know if you still have issues.

Regards,
Girish

1 Like

Thanks for your help, this helped ! however, I’m getting anathor error :
ros2 launch wall_follower start_wall_finder.launch.py
file ‘start_wall_finder.launch.py’ was not found in the share directory of package ‘wall_follower’ which is at ‘/home/user/ros2_ws/install/wall_follower/share/wall_follower’

I saw a topic with the same issue :Cannot launch my package - Rosject - #4 by girishkumar.kannan

however the guy solved it with dependency issues. for me here are the imports in my python file and all are in package.xml.

can you help with that please.
Screenshot 2024-05-14 193242
Screenshot 2024-05-14 193252

Hi @BushraAlsh ,

This is not a dependency error. You have forgotten to include the launch directory to your exports.

Since you are using Python and not C++, you need to add a few lines in your setup.py file:

  1. Add these two lines above the setuptools line.
import os               # <--- add this line here
from glob import glob   # <--- add this line here
from setuptools import setup
  1. Add the following line under data_files list, inside the setup function:
data_files=[
    ('share/ament_index/resource_index/packages',
        ['resource/' + package_name]),
    ('share/' + package_name, ['package.xml']),

    # add the following line for the compiler to find the launch files
    (os.path.join('share', package_name), glob('launch/*.launch.py'))
],

These changes will fix your problem. Let me know if you still have issues after following these.

Regards,
Girish

I appreciate your patience with me and help. However, I allready had those in the setup but still the same issue. This is my setup :
from setuptools import setup
import os
from glob import glob

package_name = ‘wall_follower’

setup(
name=package_name,
version=‘0.0.0’,
packages=[package_name],

data_files=[
('share/ament_index/resource_index/packages',
    ['resource/' + package_name]),
('share/' + package_name, ['package.xml']),

(os.path.join('share', package_name), glob('launch/*.launch.py'))

],
install_requires=[‘setuptools’],
zip_safe=True,
maintainer=‘user’,
maintainer_email=‘user@todo.todo’,
description=‘TODO: Package description’,
license=‘TODO: License declaration’,
tests_require=[‘pytest’],

entry_points={
    'console_scripts': [
        'wall_following = wall_follower.wall_following:main',
        'wall_finder = wall_follower.wall_finder:main'
    ],
},

)

I made sure I have the launch file in the correct directory as can be seen in the capture. However, when I do the colcon build I get warning :

user:~/ros2_ws$ colcon build# compiles all packages
Starting >>> wall_follower
[1.098s] WARNING:colcon.colcon_ros.prefix_path.ament:The path’/home/user/ros2_ws/install/wall_follower’ in the environmentvariable AMENT_PREFIX_PATH doesn’t exist
[1.098s] WARNING:colcon.colcon_ros.prefix_path.catkin:The path ‘/home/user/ros2_ws/install/wall_follower’ in the environment variable CMAKE_PREFIX_PATH doesn’t exist
Finished <<< wall_follower [18.5s]

Summary: 1 package finished [18.8s]

Hi @BushraAlsh ,

You can safely ignore the following WARNINGs. They do not cause any harm.

The important thing is that your compilation was successful with the following message:

You do not have to worry about the above warnings. Only Error messages needs extra work / fixing.

Your package has compiled successfully. No issues there!

Regards,
Girish

Thank you, but I still get the error when I try to launch
ros2 launch wall_follower start_wall_finder.launch.py
file ‘start_wall_finder.launch.py’ was not found in the share directory of package ‘wall_follower’ which is at ‘/home/user/ros2_ws/install/wall_follower/share/wall_follower’

Hi @BushraAlsh ,

You are definitely missing some step, which I am unable to diagnose.

Please post your complete project tree. Expand all your folders in your package on the IDE left panel and post a screenshot of that. Only the expanded folder contents, not the entire IDE.

Also, post your complete setup.py file as a markdown fenced code-block format.

Regards,
Girish

The requested screenshots are attached.
Below the code setup as requested

from setuptools import setup
import os
from glob import glob

package_name = 'wall_follower'

setup(
    name=package_name,
    version='0.0.0',
    packages=[package_name],

    data_files=[
    ('share/ament_index/resource_index/packages',
        ['resource/' + package_name]),
    ('share/' + package_name, ['package.xml']),

    (os.path.join('share', package_name), glob('launch/*.launch.py'))
],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='user',
    maintainer_email='user@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],

    entry_points={
        'console_scripts': [
            'wall_following = wall_follower.wall_following:main',
            'wall_finder = wall_follower.wall_finder:main'
        ],
    },
)


For this kind of error, it could mean two things. Please try them in this order:

  1. You have properly built and sourced the module on one web shell, but it is not working on another.
  • You need to source ~/ros2_ws/install/setup.bash on the shell where you are running the program
  1. You have not properly built and sourced the package (please go through the steps and check again):
# On web shell 1
cd ~/ros2_ws
rm -rf install/ build/ log/
colcon build
source install/setup.bash

# On any other web shell
cd ~/ros2_ws
source install/setup.bash

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.