Cannot import a python file within the same directory.

asked 2020-12-14 06:24:58 -0500

Forenkazan gravatar image

Hello

I have two scripts: caller.py and test_lib.py. I want to import test_lib.py in the caller.py to use some of the functions there. The files work perfectly when run normally:

python3 caller.py

However, when I call it using rosrun:

rosrun my_package caller.py

I get the following error

ModuleNotFoundError: No module named 'test_lib'

When I remove the import line, the program run without errors

Both scripts are in the same folder (scripts). Here they are:

caller.py

#!/usr/bin/env python 

import test_lib

if __name__ == "__main__":
     test_lib.test1()

test_lib.py

def test1():
    print('test')
edit retag flag offensive close merge delete

Comments

Both scripts are in the same folder (scripts). Here they are:

In the same folder in the source space? What folder are they in when they're installed? I think I would need to see the CMakeLists.txt and setup.py file to answer this one.

sloretz gravatar image sloretz  ( 2020-12-14 11:27:46 -0500 )edit
1

@sloretz yes they are in catkin_ws/src/PACKAGE/scripts. I did no edit the CMakeLists.txt, only added the two scripts to execute them using rosrun and did not create setup.py since I assume they should work since they work normally in python using: python3 caller.py

Forenkazan gravatar image Forenkazan  ( 2020-12-14 11:33:37 -0500 )edit

Hi @sloretz , @Forenkazan

Any solution to this? I am also facing the same issue.

HappySamuel gravatar image HappySamuel  ( 2022-03-02 03:08:48 -0500 )edit