I think this error occurs because of your settings.json which is generated from vscode extention. Check my settings.json and modify your settings.json for your ROS env. (By the way, you can find settings.json on .vscode folder)
{
"python.autoComplete.extraPaths": [
"/opt/ros/humble/lib/python3.10/site-packages",
"/opt/ros/humble/local/lib/python3.10/dist-packages",
"/usr/lib/python3.10/",
],
"python.analysis.extraPaths": [
"/opt/ros/humble/lib/python3.10/site-packages",
"/opt/ros/humble/local/lib/python3.10/dist-packages",
"/usr/lib/python3.10",
],
"files.associations": {
"unordered_map": "cpp",
"unordered_set": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"hash_map": "cpp",
"hash_set": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"bit": "cpp",
"geometry": "cpp",
"*.ipp": "cpp"
}
}
Answer from furkansariyildiz on Stack ExchangeIm trying to write python for use in turtlesim however I can’t seem to run the program due to rclpy not importing correctly. I am using vscode and whenever I use pip install it just returns an error. Not sure how to continue.
I think this error occurs because of your settings.json which is generated from vscode extention. Check my settings.json and modify your settings.json for your ROS env. (By the way, you can find settings.json on .vscode folder)
{
"python.autoComplete.extraPaths": [
"/opt/ros/humble/lib/python3.10/site-packages",
"/opt/ros/humble/local/lib/python3.10/dist-packages",
"/usr/lib/python3.10/",
],
"python.analysis.extraPaths": [
"/opt/ros/humble/lib/python3.10/site-packages",
"/opt/ros/humble/local/lib/python3.10/dist-packages",
"/usr/lib/python3.10",
],
"files.associations": {
"unordered_map": "cpp",
"unordered_set": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"array": "cpp",
"atomic": "cpp",
"strstream": "cpp",
"*.tcc": "cpp",
"bitset": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"complex": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"functional": "cpp",
"iterator": "cpp",
"map": "cpp",
"memory": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"set": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"hash_map": "cpp",
"hash_set": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"ostream": "cpp",
"shared_mutex": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cfenv": "cpp",
"cinttypes": "cpp",
"typeindex": "cpp",
"typeinfo": "cpp",
"variant": "cpp",
"bit": "cpp",
"geometry": "cpp",
"*.ipp": "cpp"
}
}
Navkaran, you said you were learning ros2 with docker, and havent had issues
I faced the same exact issue line for line, so I'm wondering what tutorials you used to start using ros2 with docker?
Hi Folks,
I have hit a bit of a silly issue here which the solution is eluding me.
I have built a workspace using “colcon” and within the src folder i have created a package using the following command.
ros2 pkg create Data_Stream --dependencies rospy --build-type ament_python
I then run VSCode within the package folder. Now when i import rospy it tells me the following error, “rospy” is not accessed Pylance.
VSCODE Extensions
-
Pylance
-
Python
-
CMake
-
ROS
Now i have checked my json file and it does have the correct paths but i could be wrong.
{
“python.autoComplete.extraPaths”: [
“/home/user1/dev_sb/install/Data_Stream/lib/python3.8/site-packages”,
“/opt/ros/foxy/lib/python3.8/site-packages”
],
“python.analysis.extraPaths”: [
“/home/user1/dev_sb/install/Data_Stream/lib/python3.8/site-packages”,
“/opt/ros/foxy/lib/python3.8/site-packages”
]
}
I have also tested this with another test.py file at the very top level of the workspace and same result.
I know i have missed something simple so any help would be great!
Thanks!
The issue was indeed with Pylance. It was missing an "additional path" to where pip had installed the projects I wanted to import. To solve the issue:
First make sure you know the location of your import; you can find it with:
$ python
>>> import modulename
>>> print(modulename.__file__)
Then, once you know the location:
- Open settings (ctrl + ,)
- Search "pylance" or find it under "Extensions > Pylance"
- Find the "Extra Paths" config item
- Use "add item" to a add a path to the parent folder of the module. It will not do any recursive tree searching
And you should be good to go!
For a further example, you can see the image above where I had added the path /home/seph/.local/lib/python2.7/ to no avail. Updating it to /home/seph/.local/lib/python2.7/site-packages/ did the trick.
I had a similar problem in my setup as I create a virtual environment per project and install Jupyter there. Instead of a global setting change in the Pylance extension, I rather prefer to follow what states its troubleshooting guide.
Suppose your virtual environment is called venv and it is inside your project folder:
- Create a folder called
.vscodeat the root of your project folder, if it does not already exist. Don't forget the dot in front of the name. - Create or edit the file
.vscode/settings.jsonand include this tag:
{
"python.analysis.extraPaths": ["./venv"]
}
- Note that the path to your virtual environment is relative to the root project folder, not the folder containing the
settings.jsonfile.
I hope this helps.
Hello the solution I found came from this site
https://cps.unileoben.ac.at/how-to-setup-ros2-in-pycharm/
in case later on it gets removed here is the text:
How to setup ROS2 in PyCharm image_pdfimage_print Basic Usage of ROS2 with PyCharm
To access code completion and syntax error handling (debugging doesn’t work in PyCharm directly) in PyCharm for ROS2 the paths have to be linked in your project structure. To do so the following steps have to be performed:
Go to Settings -> Project: -> Project Structure and Click on Add Content Root. Now add the two directories: /opt/ros//lib//site-packages and /opt/ros//local/lib//dist-packages and set them to Resources
Now the code completion and syntax error handling works for ROS2. Custom Messages & Custom Packages
To resolve import errors and add code completion in PyCharm for ROS2 projects you have to further set some directories as source roots.
First for custom messages (now called interfaces) build your project once with colcon build. Then go to install -> -> local -> lib -> -> dist-packages and mark this folder as Source with the right click drop down menue.
For custom packages you go to install -> -> lib -> -> site-packages and mark this as Source with the right click drop down menue.
Don’t forget the build your project with colcon build if you change your custom packages/modules so the code is updated in the install directory. Custom Packages and setup.py
To use custom packages in your ROS2 nodes you need to link them in your setup.py file of your ROS2 package. To do so all packages need to have a init.py file depending on your package this file is in general empty. Then you open the setup.py file and add all your packages to the list “packages”. The list should look like this:
packages = [package_name, package_name + ‘/Your_Package’, package_name+’/Your_2nd_Package’],
but also after this you have to include the correct environment variables in the configuration you are about to run in pycharm. to do this open Run > Edit Configurations... and then get the current ones from your machine by running in terminal (linux) $ printenv | grep 'ros|ROS' on other operating systems you can find them similarly and get those that have either ros or ROS in the name
So I myself found a answer just to import the rclpy module inside pycharm. The real problem here is that the interpreter in pycharm doesn't look for the ros2 modules automatically so we need to add them manually. Here are the steps to add the paths of module to make the interpreter look for it.
step 1 : Go to file > settings > python Interpreter > show all
step 2 : click on the icon with small size folders next to the filter icon 'the icon next to filter icon'
step 3 : include the paths mentioned below using the plus symbol.
/opt/ros/<distro>/bin
/opt/ros/<distro>/local/lib/python3.10/dist-packages
/opt/ros/<distro>/lib/python3.10/site-packages
/opt/ros/<distro>/lib
Reference Image for Paths
my distro is ROS2 HUMBLE, the python version depends on what python version that you are using.
step 4 : press ok and restart the Pycharm IDE
I'm still facing a error ImportError: librcl_action.so: cannot open shared object file: No such file or directory
will update this thread when I found a solution. Also refer this github page for further information github thread
UPDATE REGARDING THAT ERROR MENTIONED ABOVE:
As said before I was having problems with IDEs not recognising the rclpy library of ROS2. As by the comment yesterday I mentioned how I solved the problem by adding the interpreter paths in Pycharm IDE. The Pycharm IDE tries to run the file inside the IDE or something like that and received the same error as the title of this thread. Today I tried the code with visual studio code, surprisingly VS code recognised that rclpy library and showed all the auto complete and other things of that library. Since VS code runs every file in terminal inside the IDE, so the code works without any issues. I think this might fix your problem with that error?
On your draw_circle.py file line 3 you have the code import rclypy which is an incorrect name of the module and the reason your are getting that error, just change that line to import rclpy and your code should work.
PD: Next time please follow the guidelines for asking questions so it is easier for anyone to understand your problem and help you. :)
Looks like you have a typo here.
Change rclypy to rclpy and see if it works.