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 calledscripts
) - to me it looks like
server.py
should be a module. If so, set it up as that such that you canfrom 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.