Import problem with python and ros

New to this and I am having an issue understanding how to add all the parts needed for my program to work.
I’ve made a music player using gstramer in a ros package but when I create and try to run the snap I am getting import errors.

This is the beginning of the music player program that isnt importing:

import gi
gi.require_version('Gst', '1.0')
from gi.repository import GObject
from gi.repository import Gst as gst
from gi.repository import GLib

And this is my snapcraft.yaml:

name: musicplayer 
version: '0.1' 
summary: music player
description: |
  the musicplayer

grade: stable 
confinement: strict

parts:
  workspace:
    plugin: catkin
    rosdistro: kinetic
    catkin-packages: [music_player]

apps:
  musicplayer:
    command: roslaunch music_player music_player.launch
    plugs: [network, network-bind]

Can you please provide the exact import errors you get? @kyrofa has some experience with ROS and may be able to help you provide him with enough info.

Sounds like you’re missing PyGObject, but as @niemeyer said, it’s difficult to know without more information. Nothing in your YAML will include PyGObject into the snap for you: do you have it as a rosdep dependency, perhaps? Or did you simply forget to put it into the snap? If your code is public, that would help as well.

Traceback (most recent call last):
File “/snap/musicplayer/x1/opt/ros/kinetic/lib/music_player/music_player_server_node”, line 4, in
from music_player.music_player_server import MusicPlayerServer
File “/snap/musicplayer/x1/opt/ros/kinetic/lib/python2.7/dist-packages/music_player/music_player_server.py”, line 5, in
from music_player._music_player import MusicPlayer
File “/snap/musicplayer/x1/opt/ros/kinetic/lib/python2.7/dist-packages/music_player/_music_player.py”, line 1, in
import gi
ImportError: No module named gi

Any chance I can at least see music_player’s package.xml?

buildtool_depend>catkin</buildtool_depend
build_depend>actionlib</build_depend
build_depend>actionlib_msgs</build_depend
build_depend>rospy</build_depend
build_depend>std_msgs</build_depend
build_depend>message_generation</build_depend
run_depend>actionlib</run_depend
run_depend>actionlib_msgs</run_depend
run_depend>rospy</run_depend
run_depend>std_msgs</run_depend
run_depend>message_runtime</run_depend

Yeah I don’t see anything that’s pulling in PyGObject, unless rospy depends upon it, which would surprise me. Have you tried building this ROS package in a clean environment, ignoring snaps? I doubt it works there either. You need to either make sure all your dependencies are outlined in the package.xml (which then relies on rosdep to fetch/install them) or add them to the snapcraft.yaml themselves.

the ROS package works fine outside of trying to create it in a snap. Is there anywhere to find more information or tutorials on adding them in the snapcraft.yaml itself. I v\have search but not been able to find much

I had the same problem recently and it took quite some time to figure all dependencies. Your configuration is missing GStreamer libs, Python GObject Introspection libs as well as proper environment variables.
Here is what your snapcraft.yaml should look like:

name: musicplayer 
version: '0.1' 
summary: music player
description: |
  the musicplayer

grade: stable 
confinement: strict

parts:
  workspace:
    plugin: catkin
    rosdistro: kinetic
    catkin-packages: [music_player]
    stage-packages:
      - gstreamer1.0-plugins-base
      - gir1.2-gstreamer-1.0
      - gir1.2-gst-plugins-base-1.0
      - python-gi
      - python-gst-1.0

apps:
  musicplayer:
    command: roslaunch music_player music_player.launch
    plugs: [network, network-bind]
    environment:
      GST_PLUGIN_PATH: $SNAP/usr/lib/x86_64-linux-gnu/gstreamer-1.0
      GST_PLUGIN_SCANNER: $SNAP/usr/lib/x86_64-linux-gnu/gstreamer1.0/gstreamer-1.0/gst-plugin-scanner
      GI_TYPELIB_PATH: $SNAP/usr/lib/girepository-1.0:$SNAP/usr/lib/x86_64-linux-gnu/girepository-1.0

Obviously environment variables like that would only work on x64 systems, you may want to write a script that customize them appropriately for target system.