Ask Your Question
0

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

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

mutantRobot gravatar image

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

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 -0600

mutantRobot gravatar image

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

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 -0600

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 -0600 )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 -0600 )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 -0600 )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 -0600 )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 -0600 )edit

I confirm the mape_misc was generated under catkin_ws/build

mutantRobot gravatar image mutantRobot  ( 2017-08-28 06:57:44 -0600 )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 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

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

Seen: 5,680 times

Last updated: Aug 30 '17