Ask Your Question
0

Unable to use functions from custom imported module

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

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

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 setup.py, you can choose the destination
   catkin_install_python(PROGRAMS
    src/cmdvel_adapterv1.0
    src/server.py
    DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION}
  )

But now, when I try to use any function of server.py 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)
    rospy.spin()



if __name__ == '__main__':

    try:
        rospy.init_node('cmdvel_adapter_v1_0')
        function()

    except rospy.ROSInterruptException:
        pass

in my rosnode, I get error -

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

This is the Server.py 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()
    server.bind((host,port))
    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

Comments

1

Please edit your question and add the CMakeLists.txt, setup.py 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).

Thanks.

mgruhler gravatar image mgruhler  ( 2021-01-05 02:51:19 -0600 )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 , server.py module and my main code. Both the Server.py and the main code are in the ~/catkin_ws/src/rospackage/src/ .

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

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 server.py 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 -0600 )edit
1

with catkin_install_python you should only install executable scripts

this is indeed the most likely cause of the problem.

server.py 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 -0600 )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 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

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

Hi there, I found a solution here : https://roboticsbackend.com/ros-impor...

edit :

As mentioned in the blog : Step 1: create a setup.py 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(
    packages=['myPkg'],
    package_dir={'': 'src'}
)
setup(**d)

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

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

Tried it, and it worked perfectly for me.

Hope it helps.

edit flag offensive delete link more

Comments

2

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 -0600 )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 -0600 )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

2 followers

Stats

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

Seen: 273 times

Last updated: Jan 07 '21