How to (correctly) install and import pymavlink using PIP

I am using OS X Yosemite and am trying to set up pymavlink with pip and python 2.7. Typed sudo pip install pymavlink

and it installs nicely. Tried importing it with

from pymavlink import mavlinkv10 as mavlink


and the interpreter throws this error:

File "/Library/Python/2.7/site-packages/pymavlink/", line 10, in from ... generator.mavcrc import x25crc ValueError: Attempted relative import outside of top level package

I think I understand the error and can manually "fix" it by changing the implicit import "... generator" to the explicit "pymavlink.generator". My question is what is the correct way to install and import this package using pip. I may not be the first person to have this problem :)


source to share

2 answers

It doesn't seem like anyone is clicking this question with a tutorial answer, so I'll go ahead and post how I combined installing python mushlink in a makeshift listing on a Raspberry Pi and hopefully someone will post a legitimate solution in the future on how do it correctly with PIP. Any comments / corrections are welcome!

install pymavlink by typing "sudo pip install pymavlink". This will install the pymavlink module (and the file, which you will have to change) to the following directory on your pi "/usr/local/lib/python2.7/dist-packages/pymavlink/"

import the pymavlink module with the following line at the beginning of your Python code "from pymavlink import mavlinkv10 as mavlink"

When running the pymavlink example code from the pymavlink repository, the interpreter first throws the following error:

File "/Library/Python/2.7/site-packages/pymavlink/"
in from ...generator.mavcrc import x25crc 
ValueError: Attempted relative import beyond toplevel package


It looks like is trying to do relative imports from the three parent directories "up" (I guess?). Since the "generator" directory is actually a child directory of, I changed the following in

changed this:  from ...generator.mavcrc import x25crc
to this:       from generator.mavcrc import x25crc


Next, the interpreter will throw the following error when running the mavlink python code that tries to create (encode) a new mavlink message:

File "/usr/local/lib/python2.7/dist-packages/pymavlink/", line 1053, in pack
return MAVLink_message.pack(self, mav, 137, struct.pack('<QI', self.time_unix_usec, self.time_boot_ms))
File "/usr/local/lib/python2.7/dist-packages/pymavlink/", line 110, in pack
File "/usr/local/lib/python2.7/dist packages/pymavlink/generator/", line 18, in accumulate
tmp = b ^ (accum & 0xff)
TypeError: unsupported operand type(s) for ^: 'str' and 'int'


It looks like mavlinkv10 doesn't work with the extra crc computation in the pack () function. You can "fix" this error by disabling additional crc files in the mavlinkv10 pack () function. Comment out these 2 lines in

#if True: # using CRC extra
#    crc.accumulate(chr(crc_extra))


The last thing. You also need to disable crc2 computation in the decode () function in Comment these lines in the file:

#if True: # using CRC extra 
#    crc2.accumulate(chr(crc_extra))
#if crc != crc2.crc:
#    raise MAVError('invalid MAVLink CRC in msgID 


At this point, you should be able to run the pymavlink example code, although I don't understand how turning off the "extra CRC" calculation would affect mavlink reliability (perhaps not positively).



Instead of using

from pymavlink import mavlinkv10 as mavlink



from pymavlink.dialects.v10 import common as mavlink


replacing common

with any dialect you may want (see the XML files at for all options)



All Articles