ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Unable to use functions from custom imported module

asked 2021-01-05 02:47:46 -0500

updated 2021-01-05 07:52:15 -0500

Hi there, I'm a novice experimenting on making and using custom modules (python) having functions that is required for my rosnode. I can do the same in normal python, but I am struggling with the import. After I added the python script in CMakeLists.xml as executable script(python) , import didn't show error. Edited CMakeLists.xml :

## Mark executable scripts (Python etc.) for installation
## in contrast to, you can choose the destination

But now, when I try to use any function of from cmdvel_adapterv1.0 :

#!/usr/bin/env python3
import server
import rospy
from std_msgs.msg import String

def callback(data):


def function():

    port = 8081
    s = server.init_server(port)
    sub = rospy.Subscriber('cmdvel_socket_stream', String, callback)

if __name__ == '__main__':


    except rospy.ROSInterruptException:

in my rosnode, I get error -

    s = server.init_server(port)
AttributeError: module 'server' has no attribute 'init_server'

This is the module :-

import os
import socket

def dow():
    print("its accessing")

def get_ip():

    ip = socket.gethostname()
    return ip

def init_server(port):

    host = get_ip()
    server = socket.socket()
    print('binded to ' + str(host) +':' + str(port))
    return server

What am I doing wrong here? This method worked outside ros, why not in ros. Help me.

Thanks in advance Paul Pavish

edit retag flag offensive close merge delete



Please edit your question and add the CMakeLists.txt, and source code snippets (of the module and the node). Make sure to only include the relevant parts (remove any unnecessary comments, keep the import and how you call the function, but not the conent).


mgruhler gravatar image mgruhler  ( 2021-01-05 02:51:19 -0500 )edit

@mgruhler, Hi there, thanks for the response. I updated the question with your suggested edits with important parts of my code in CMakeLists.xml , module and my main code. Both the and the main code are in the ~/catkin_ws/src/rospackage/src/ .

Paul Pavish gravatar image Paul Pavish  ( 2021-01-05 07:55:56 -0500 )edit

I cannot reproduce that. Please clarify how you run this that you get the error (from devel or install space? Which commands?)

Seems like a name clash to me, but I can only guess. This might depend on what other packages/modules you have installed or in there.

Some general remarks:

  • with catkin_install_python you should only install executable scripts. those usually should not be included in other files (and should rather be in a directory called scripts)
  • to me it looks like should be a module. If so, set it up as that such that you can from myrospackage import server. Have a look at this doc page for more information.
mgruhler gravatar image mgruhler  ( 2021-01-05 08:57:14 -0500 )edit

with catkin_install_python you should only install executable scripts

this is indeed the most likely cause of the problem. should be part of a package. It's not a stand-alone script.

This is also not really ROS-specific (apart from the one Catkin command there).

gvdhoorn gravatar image gvdhoorn  ( 2021-01-05 09:46:16 -0500 )edit

Thankyou @mgruhler and @gvdhoom , With your keyword suggestions I was able to find a solution. Again, thanks for your response.

Paul Pavish gravatar image Paul Pavish  ( 2021-01-06 12:20:17 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-01-06 12:18:33 -0500

updated 2021-01-07 00:38:13 -0500

Hi there, I found a solution here :

edit :

As mentioned in the blog : Step 1: create a 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(
    package_dir={'': 'src'}

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 and save it as an empty file.

Step 4: In the same folder, Create and save the python module files ( 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 using the line from myPkg import myModule.

Tried it, and it worked perfectly for me.

Hope it helps.

edit flag offensive delete link more



Link-only answers are highly discouraged.

Could you please post the relevant steps you took as part of your answer?

gvdhoorn gravatar image gvdhoorn  ( 2021-01-06 14:08:04 -0500 )edit

@Paul Pavish great that you found a solution. For reference, those steps are basically what is described in the catkin docs, though there it is more of a text block then a step by step guide.

mgruhler gravatar image mgruhler  ( 2021-01-07 01:45:18 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2021-01-05 02:47:46 -0500

Seen: 928 times

Last updated: Jan 07 '21