ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

rospy init_node() inside imported file

asked 2017-07-17 18:57:02 -0500

zplizzi gravatar image

I'd like to perform a rospy.init_node() inside a file imported by another file. However, this doesn't seem to work - the program hangs whenever I try this.

Example (broken, both files in same folder):

import test

   import rospy

Running python results in the program hanging. However, running python works.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2017-07-18 08:50:53 -0500

updated 2017-07-18 08:51:12 -0500

I have a similar project, it works like that:

  1. Inside the project folder, I have a folder called packages, with a inside. It's empty, just to make the module available
  2. In the packages folder yet, a file called, where I have:

    import rospy as _rospy from std_msgs.msg import String as _String, Empty as _Empty

    def init():

        _rospy.init_node('controller', anonymous=True)
        global _sub
        _sub = _rospy.Subscriber("/model_name", _String, _nameCallback)
        global _pub
        _pub = _rospy.Publisher(_controllerName+'/encoder_updated',_Empty)
  3. In the root folder, my script that imports the initialize library:

    import packages.initialize


edit flag offensive delete link more


Yep, this works - since the init_node() call is in a method called by the main file, it seems to work. What I can't figure out is why it doesn't work in the example I described - where init_node() is called implicitly during the import. This method works, but still curious why the other doesn't.

zplizzi gravatar image zplizzi  ( 2017-07-18 11:23:06 -0500 )edit

I think the answers for this question can explain better. The question is the opposite of yours, but there are some explanation there.

marcoarruda gravatar image marcoarruda  ( 2017-07-18 11:33:05 -0500 )edit

Yep, I understand how that works. Python will run all the code in an imported file upon import. What my question is asking is why the init_node() function doesn't work when run in that way (versus being run directly from the main script).

zplizzi gravatar image zplizzi  ( 2017-07-18 13:01:19 -0500 )edit

answered 2019-11-18 14:17:28 -0500

MartinK gravatar image

Not sure where this comes from. My workaround is by importing the module from imp. No need to modify the module itself.

import imp
test = imp.load_source("test", "")
edit flag offensive delete link more

answered 2017-08-02 10:17:08 -0500

naveedhd gravatar image

updated 2017-08-02 10:17:30 -0500

Althought this is mentioned here that if init_node is called with disable_signals=True we can call init_node from separate thread, but it still gets stuck here. (os.getpid() is different for main and test)

Now you can go down in impl/ and impl/ to look deeper of how the different threads affect the node registration with the master.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2017-07-17 18:57:02 -0500

Seen: 1,515 times

Last updated: Nov 18 '19