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

import python modules failed...what am I missing?

asked 2017-08-28 02:18:30 -0500

mutantRobot gravatar image

updated 2017-08-29 02:10:05 -0500

Hello guys,

I have a question about importing my own python modules from a package I wrote.

I am using ROS Indigo on Ubuntu 14.04.

According to

  1. setup.py manual
  2. install python

Assuming I have a package called mape_misc under catkin_ws/src,

the structure of it is :

src/mape_misc/
├── CMakeLists.txt
    ├── package.xml
    ├── setup.py
    └── src
        └── mape_misc
            ├── __init__.py
            ├── MAPEopcode.py

I wanted to import mape_misc.MAPEopcode in another python packages. Supposing there is a class OPCODE in MAPEopcode.py

However, in the other package, I got

from mape_misc.MAPEopcode import OPCODE
ImportError: No module named mape_misc.MAPEopcode

I'm stuck here right now, the content of my setup.py is :

#!/usr/bin/env python
from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

setup_args = generate_distutils_setup(
    package=['mape_misc'],
    package_dir={'': 'src'},
)

setup(**setup_args)

In mape_misc, CMakeLists.txt is:

cmake_minimum_required(VERSION 2.8.3)
project(mape_misc)
find_package(catkin REQUIRED COMPONENTS
  rospy
)
catkin_python_setup()

catkin_package(
  CATKIN_DEPENDS rospy
)

I don't think I need to have a install() because I just want to use modules.

In MAPEopcode I just defined a Enum.

I've tried to clean the build and redo catkin_make, but still no luck.

Thanks for any advice!

Edit 1: I thought for any correct import, catkin_ws/src/mape_misc/src should be in PYTHONPATH. However, after I built everything and source devel/setup.bash, echo $PYTHONPATH showed only: /home/xbot/catkin_ws/devel/lib/python2.7/dist-packages:/opt/ros/indigo/lib/python2.7/dist-packages:/home/xbot/.local/lib/python2.7/site-packages:/usr/local/lib/python2.7/dist-packages

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2017-08-30 00:04:17 -0500

mutantRobot gravatar image

updated 2017-08-30 00:04:38 -0500

OK I finally found the problem ------- a really stupid mistake in setup.py, which should contain:

packages = ['xxxx']

not

package = ['xxxx']

Now everything is working.

edit flag offensive delete link more
-1

answered 2017-08-28 03:18:02 -0500

naveedhd gravatar image

You need to specify the modules to import in __init__.py. Check if you have something like from . import MAPEopcode in your __init__.py.

edit flag offensive delete link more

Comments

Thank you. In most time I left it empty but I tried your solution. However it still didn't work. I also noticed that in my catkin_ws/devel/lib/python-2.7/dist-packages/ has nothing about mape_misc, so I guessed there might be something else wrong?

mutantRobot gravatar image mutantRobot  ( 2017-08-28 06:15:27 -0500 )edit

In the structure of directory posted in question, the CMakeLists.txt of package is missing, but I'm assuming it's there. You can also verify by remaking from scratch and see the log if the package is being built.

naveedhd gravatar image naveedhd  ( 2017-08-28 06:25:58 -0500 )edit

I'm a bit confused...there was a CmakeLists.txt in top level catkin_ws/src/mape_misc/ . Do you mean to create another one in catkin_ws/src/mape_misc/src/mape_misc/ ?

mutantRobot gravatar image mutantRobot  ( 2017-08-28 06:35:22 -0500 )edit

sorry for confusion. I mean there should be one under catkin_ws/src that is created by catkin_init_workspace. Other one is under catkin_ws/src/mape_misc that your have written, and this should be along with package.xml and setup.py.

naveedhd gravatar image naveedhd  ( 2017-08-28 06:40:17 -0500 )edit

Yes there is one under catkin_wx/src, CMakeLists.txt -> /opt/ros/indigo/share/catkin/cmake/toplevel.cmake.

mutantRobot gravatar image mutantRobot  ( 2017-08-28 06:44:49 -0500 )edit

I confirm the mape_misc was generated under catkin_ws/build

mutantRobot gravatar image mutantRobot  ( 2017-08-28 06:57:44 -0500 )edit

You don't need to specify the modules to import in the __init__.py. Simply having that file (even an empty one) makes that directory a Python package. Then use from mape_misc import MAPEopcode.

jayess gravatar image jayess  ( 2018-01-03 23:08:43 -0500 )edit

Question Tools

2 followers

Stats

Asked: 2017-08-28 02:18:30 -0500

Seen: 7,667 times

Last updated: Aug 30 '17