This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Linux/AM3358: Python Issue in SDK 5.x

Part Number: AM3358


Tool/software: Linux

AM335x BBB, Linux SDK 5.01

I have a Python script that ran fine in SDK 3.03.  It won't work in SDK 5.01.  It looks like SDK 3.03 used Python 2 while SDK 5.x has both Python 2 and Python 3.  So in that regard I was expecting things to still work.  Here's the error that I get:

root@am335x-evm:~# uname -a
Linux am335x-evm 4.14.67-gd315a9bb00 #1 PREEMPT Fri Oct 12 09:30:01 CDT 2018 armv7l GNU/Linux
root@am335x-evm:~# neard
root@am335x-evm:~# test/test-adapter powered nfc0 on
Traceback (most recent call last):
File "test/test-adapter", line 4, in <module>
import dbus
ImportError: No module named dbus

And here's a snippet of the start of that test/test-adapter script:

#!/usr/bin/python

import sys
import dbus
import neardutils

I have tried changing the first line to python3 which actually gets passed this specific error, but it seems to introduce so many other errors that I don't think that's the right solution.  Is there something that's missing from the SDK to fully support Python 2 and Python 3?

  • I managed to get it to work, though I'd still love someone to comment that has deeper knowledge of Python if possible.

    I went down the path of changing all the shebangs in various *.py files to reference "python3" instead of "python".  There were a bunch of minor formatting changes that needed to be made after that.  Things like adding parenthesis to print statements, updating syntax for exception handling, etc.  Here's my complete list of changes: 

    diff --git a/home/brad/test/__pycache__/neardutils.cpython-35.pyc b/home/brad/test/__pycache__/neardutils.cpython-35.pyc
    new file mode 100644
    index 0000000..ef31c77
    Binary files /dev/null and b/home/brad/test/__pycache__/neardutils.cpython-35.pyc differ
    diff --git a/test/bt-handover b/home/brad/test/bt-handover
    index 42d66bf..87ad5d7 100755
    --- a/test/bt-handover
    +++ b/home/brad/test/bt-handover
    @@ -1,4 +1,4 @@
    -#!/usr/bin/python
    +#!/usr/bin/python3
     
     import os
     import sys
    diff --git a/test/handover-agent b/home/brad/test/handover-agent
    index 7f2ac23..871094a 100755
    --- a/test/handover-agent
    +++ b/home/brad/test/handover-agent
    @@ -1,4 +1,4 @@
    -#!/usr/bin/python
    +#!/usr/bin/python3
     
     import gobject
     
    diff --git a/test/monitor-near b/home/brad/test/monitor-near
    index f3ad021..216e389 100755
    --- a/test/monitor-near
    +++ b/home/brad/test/monitor-near
    @@ -1,4 +1,4 @@
    -#!/usr/bin/python
    +#!/usr/bin/python3
     
     from __future__ import absolute_import, print_function, unicode_literals
     
    diff --git a/test/ndef-agent b/home/brad/test/ndef-agent
    index 1cfeb03..937fbfc 100755
    --- a/test/ndef-agent
    +++ b/home/brad/test/ndef-agent
    @@ -1,4 +1,4 @@
    -#!/usr/bin/python
    +#!/usr/bin/python3
     
     import gobject
     
    diff --git a/test/neardutils.py b/home/brad/test/neardutils.py
    index d93ae63..04af0d5 100644
    --- a/test/neardutils.py
    +++ b/home/brad/test/neardutils.py
    @@ -17,7 +17,7 @@ def find_adapter(pattern=None):
     
     def find_adapter_in_objects(objects, pattern=None):
     	bus = dbus.SystemBus()
    -	for path, ifaces in objects.iteritems():
    +	for path, ifaces in objects.items():
     		adapter = ifaces.get(ADAPTER_INTERFACE)
     		if adapter is None:
     			continue
    @@ -31,7 +31,7 @@ def find_device(pattern=None):
     
     def find_device_in_objects(objects, pattern=None):
     	bus = dbus.SystemBus()
    -	for path, ifaces in objects.iteritems():
    +	for path, ifaces in objects.items():
     		device = ifaces.get(DEVICE_INTERFACE)
     		if device is None:
     			continue
    @@ -45,7 +45,7 @@ def find_tag(pattern=None):
     
     def find_tag_in_objects(objects, pattern=None):
     	bus = dbus.SystemBus()
    -	for path, ifaces in objects.iteritems():
    +	for path, ifaces in objects.items():
     		tag = ifaces.get(TAG_INTERFACE)
     		if tag is None:
     			continue
    @@ -59,7 +59,7 @@ def find_record(pattern=None):
     
     def find_record_in_objects(objects, pattern=None):
     	bus = dbus.SystemBus()
    -	for path, ifaces in objects.iteritems():
    +	for path, ifaces in objects.items():
     		record = ifaces.get(RECORD_INTERFACE)
     		if record is None:
     			continue
    @@ -80,14 +80,14 @@ def dump_record(record_path):
     			val = unicode(properties[key])
     		else:
     			val = str(properties[key])
    -		print "      %s = %s" % (key, val)
    +		print ("      %s = %s" % (key, val))
     
     def dump_all_records(path):
     	bus = dbus.SystemBus()
     	om = dbus.Interface(bus.get_object(SERVICE_NAME, "/"),
     					"org.freedesktop.DBus.ObjectManager")
     	objects = om.GetManagedObjects()
    -	for path, interfaces in objects.iteritems():
    +	for path, interfaces in objects.items():
     		if RECORD_INTERFACE not in interfaces:
     			continue
     
    diff --git a/test/test-adapter b/home/brad/test/test-adapter
    index 173519e..63d34f8 100755
    --- a/test/test-adapter
    +++ b/home/brad/test/test-adapter
    @@ -1,4 +1,4 @@
    -#!/usr/bin/python
    +#!/usr/bin/python3
     
     import sys
     import dbus
    @@ -70,8 +70,8 @@ if (sys.argv[1] == "powered"):
     			value = dbus.Boolean(sys.argv[3])
     		try:
     			adapter.Set("org.neard.Adapter", "Powered", value)
    -		except dbus.DBusException, error:
    -			print "%s: %s" % (error._dbus_error_name, error.message)
    +		except dbus.DBusException as error:
    +			print ("%s: %s" % (error._dbus_error_name, error.message))
     	sys.exit(0)
     
     if (sys.argv[1] == "poll"):
    @@ -90,14 +90,14 @@ if (sys.argv[1] == "poll"):
     
     			try:
     				adapter.StartPollLoop(mode)
    -			except dbus.DBusException, error:
    -				print "%s: %s" % (error._dbus_error_name, error.message)
    +			except dbus.DBusException as error:
    +				print ("%s: %s" % (error._dbus_error_name, error.message))
     
     		elif (sys.argv[3] == "off"):
     			try:
     				adapter.StopPollLoop()
    -			except dbus.DBusException, error:
    -				print "%s: %s" % (error._dbus_error_name, error.message)
    +			except dbus.DBusException as error:
    +				print ("%s: %s" % (error._dbus_error_name, error.message))
     
     		else:
     			usage()
    diff --git a/test/test-device b/home/brad/test/test-device
    index 1c90ef1..1ca894d 100755
    --- a/test/test-device
    +++ b/home/brad/test/test-device
    @@ -1,4 +1,4 @@
    -#!/usr/bin/python
    +#!/usr/bin/python3
     
     import sys
     import dbus
    diff --git a/test/test-tag b/home/brad/test/test-tag
    index cb2c9e8..7e67950 100755
    --- a/test/test-tag
    +++ b/home/brad/test/test-tag
    @@ -1,4 +1,4 @@
    -#!/usr/bin/python
    +#!/usr/bin/python3
     
     import sys
     import dbus
    @@ -46,7 +46,7 @@ if (len(sys.argv) < 2):
     if (sys.argv[1] == "list"):
     	if (len(sys.argv) < 3):
     		objects = neardutils.get_managed_objects()
    -		for path, interfaces in objects.iteritems():
    +		for path, interfaces in objects.items():
     			if "org.neard.Tag" not in interfaces:
     				continue
     
    @@ -70,7 +70,7 @@ if (sys.argv[1] == "list"):
     if (sys.argv[1] == "dump"):
     	if (len(sys.argv) < 3):
     		objects = neardutils.get_managed_objects()
    -		for path, interfaces in objects.iteritems():
    +		for path, interfaces in objects.items():
     			if "org.neard.Tag" not in interfaces:
     				continue
     
    @@ -127,7 +127,7 @@ def write_uri(args):
     		data["Type"] = "URI"
     		data["URI"] = args[0]
     
    -	print data
    +	print( data )
     
     	tag.Write(data)
     
    @@ -169,7 +169,7 @@ if (sys.argv[1] == "write"):
     	sys.exit(0)
     
     if (sys.argv[1] == "deactivate"):
    -	print sys.argv[2]
    +	print( sys.argv[2] )
     	if (len(sys.argv) != 2):
     		tag = neardutils.find_tag(sys.argv[2])
     		tag.Deactivate()
    

  • Brad,

    Sorry, I'm not a python expert. In some of our docs for python3, we list the following as the way to switch from the default 2 to 3 with these commands:

    root@am57xx-evm:/usr/bin# ln -sf python3 python.python
    root@am57xx-evm:/usr/bin# ln -sf python3-config python-config.python

    I wonder if they would have provided a simpler fix for you.