ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Can't find python script of different ros package in Pycharm

asked 2018-03-22 07:36:29 -0600

Hello,

I was following this guide to be able to access a python script from one ros package in another one. I am trying to execute the following script in Pycharm:

#!/usr/bin/env python

import rospy
from robot_control.moveclass import MoveClass
if __name__ == '__main__':
    rospy.init_node("test")
    mc = MoveClass()
    mc.move_cart_rel((0.0, 0.0, -0.1, 0, 0, 0))

Executing the script works fine and pycharm is able to resolve the rospy import, but can Not resolve from robot_control.moveclass import MoveClass.

I also followed this guide to get Pycharm running for ROS. I really dont know, what I did wrong. This is the content of my setup.py file:

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

# fetch values from package.xml
setup_args = generate_distutils_setup(
    packages=['robot_control'],
    package_dir={'': 'src'},
)
setup(**setup_args)

The setup.py file is located in the catkin package robot_control and the __init__.py and moveclass.py files are located relative to that catkin package in src/robot_control.

How am I able to get Pycharm to resolve my script, which I wanna use in multiple other packages?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2018-03-27 06:13:28 -0600

dotcom gravatar image

updated 2018-04-04 07:35:57 -0600

See the better solution on EDIT and EDIT2

Finally "solved" ! I'm not sure if this is a limitation of catkin or if it's the intended behavior. Here's what I observed:

  • Under External Libraries in pycharm, you can find the libraries that the software can find, and it gets the ROS libraries via PYTHONPATH.
  • If you find and check under your robot_control library, you will see that only the __init__.py file (and possibly messages, services) are listed, but not the modules of your package. This is why pycharm can't find the files - catkin is not installing them

One way of solving:

  1. Change your catkin build configuration to also install (if using catkin-python-tools), or perform catkin_make install for the packages you want to import. Now, if you search under the install space, you will notice that your modules are actually there as well.
  2. Add install/lib/python2.7/dist-packages/ to your PYTHONPATH. To do this, either source install/setup.bash before running pycharm, or add them manually in Settings->Project->Project Interpreter->Gear->Show All->(Last Icon, it's a dir tree)

Now pycharm will find your modules. You may also want to edit the __init__.py file of your modules, see here a good reference: http://mikegrouchy.com/blog/2012/05/b...


EDIT

Same issue reported here. It is Pycharm's fault of not parsing catkin's __init__.py file to go find the available modules. The solution is provided in that link, which for me is better than my previous solution, but very tedious.

EDIT2

Solution pasted (adapted) here in case it gets lost:

  1. Open PyCharm's settings
  2. Open your project on the left of the settings window
  3. Open Project Structure
  4. Select your project
  5. Click on Add Content Root
  6. Add the root of your package, where CMakeLists is located
  7. Click on OK, the package will now show up on the Project Tree view on the left
  8. On the Project view, find the top-most src directory of the module you want to add, Right-Click -> Mark Directory As -> Source Root
edit flag offensive delete link more

Comments

Thanks for your answer.

I also prefer the solution you mentioned here. It worked perfectly for me.

Thanks for your help.

nmelchert gravatar image nmelchert  ( 2018-03-29 02:42:03 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2018-03-22 07:36:29 -0600

Seen: 3,248 times

Last updated: Apr 04 '18