0
$\begingroup$

Rosanswers logo

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


Originally posted by Paul Pavish on ROS Answers with karma: 40 on 2021-01-05

Post score: 0


Original comments

Comment by mgruhler on 2021-01-05:
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.

Comment by Paul Pavish on 2021-01-05:
@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/ .

Comment by mgruhler on 2021-01-05:
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.

Comment by gvdhoorn on 2021-01-05:\

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

Comment by Paul Pavish on 2021-01-06:
Thankyou @mgruhler and @gvdhoom , With your keyword suggestions I was able to find a solution. Again, thanks for your response.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

Hi there, I found a solution here : https://roboticsbackend.com/ros-import-python-module-from-another-package/

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.


Originally posted by Paul Pavish with karma: 40 on 2021-01-06

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by gvdhoorn on 2021-01-06:
Link-only answers are highly discouraged.

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

Comment by mgruhler on 2021-01-07:
@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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.