1
$\begingroup$

I am new to ROS and currently doing the wiki tutorials of ROS. I am using docker on Mac M1. this is the code that I used on VS Code. I did catkin_make and source devel/setup.bash and it showed no error.

 #!/usr/bin/env python
# license removed for brevity
import rospy
from std_msgs.msg import String

def talker():
    pub = rospy.Publisher('chatter', String, queue_size=10)
    rospy.init_node('talker', anonymous=True)
    rate = rospy.Rate(10) # 10hz
    while not rospy.is_shutdown():
        hello_str = "hello world %s" % rospy.get_time()
        rospy.loginfo(hello_str)
        pub.publish(hello_str)
        rate.sleep()

if __name__ == '__main__':
    try:
        talker()
    except rospy.ROSInterruptException:
        pass

but on VS Code it shows

Import "rospy" could not be resolved

The files for rospy and std_masgs.msg are available on the docker image under the folder of python3.

After trying to find the path on terminal, it still gives me an error. It is probably because the Python folder I downloaded from the docker container isn't saved there. but selecting the folder manually is not enabling me to set it as an interpreter. I feel like I have to add the required libraries to the current interpreter I am using.

Any help is appreciated.

$\endgroup$

2 Answers 2

1
$\begingroup$

It just sounds like your paths in VSCode aren't set correctly, or maybe you have multiple versions of Python installed and VSCode is looking at the incorrect installation.

From this answer to Import "rospkg" could not be resolved

I think that you have not updated the python interpreter path in VS code.

When I try to run node with this import, rospkg works fine.

In your terminal, you have to type which python then you will get the path of your interpreter.

Now, press Ctrl+Shift+P in VS code and click on Select Interpreter. Try to set the path that you got from your terminal as a python path. I think it will work and solve your problem.

$\endgroup$
2
  • $\begingroup$ the terminal which is executing the container returns nothing on typing which python whereas in my native terminal it is a path and copying that path doesn't solve the issue. the path it showed- /Users/mayanksethia/.pyenv/shims/python but this the path that i saved my python folder from docker container on. selecting manually the python folder doesn't allow the select interpreter option in vscode. $\endgroup$
    – Mayank
    Jun 24, 2023 at 8:45
  • $\begingroup$ Is the VSCode running in the docker? Is your home directory also in the docker? This seems unlikely - unless you are using the docker as a separate dev env. I'm not an expert on docker, but isn't docker a self contained workspace, and so, shouldn't everything (VScode, and the path directories) all be running in the docker? Or, better still, develop the code locally and when it is running, then move the required files to the docker. Two separate/disparate workspaces seems to be the issue - although I could well be wrong. $\endgroup$ Jun 24, 2023 at 9:27
1
$\begingroup$

Depending on how you have setup VSCode it's likely that the internal environment is not inheriting the environment that you have setup on the console. You will need to update you VSCode environment to source the setup.bash when VSCode creates the environment.

I personally only use the editor and run everything from the command line.

There's also a ROS extension for VSCode to help with environment management.

$\endgroup$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.