raspberry pi: reader found, nfc module not found

Asked by Rupert

Many thanks for what appears to be a great library.

Adafruit reader is detected:

pi@raspberrypi ~/nfcpy/nfcpy-0.9.2/examples $ python tagtool.py --device tty:AMA0:pn53x show
[nfc.clf] searching for reader with path 'tty:AMA0:pn53x'
[nfc.clf] using NXP PN532 at /dev/ttyAMA0
[main] touch a tag

However Python has a problem with importing the module nfc.

If I put a file called "test.py" containing one line "import nfc" in the directory as below

pi@raspberrypi ~/nfcpy/nfcpy-0.9.2/examples $ python test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import nfc
ImportError: No module named nfc

Similarly with a change of directory, or from the python console.

pi@raspberrypi ~/nfcpy $ python test.py
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    import nfc
ImportError: No module named nfc

If I change the file "test.py" to include:

import sys
print sys.path

The following results:

pi@raspberrypi ~/nfcpy $ python test.py
['/home/pi/nfcpy', '/usr/local/lib/python2.7/dist-packages/distribute-0.7.3-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/pymodules/python2.7']

Why doesn't Python find the "nfc" module?

What can I do?

Many thanks in advance,


(Raspbery Pi 2 running Raspbian)

Question information

English Edit question
nfcpy Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
Revision history for this message
Rupert (rupert-nospam) said :

A few more hours and found a solution that worked.

In the directory


created a file named


containing one single line:


NOW the module NFC imports in python.

It still makes no sense to me that Python could not find the modules with a sys.path containing '/home/pi/nfcpy'.

Perhaps somebody else knows?

Many thanks.

Revision history for this message
Stephen Tiedemann (stephen-tiedemann) said :

The nfcpy library does not automatically become part of the Python installation when unpacked (or branched) into some user directory. The "import nfc" statement generally works from the directory where the library exisits; it is the "nfc/" directory that contains the "nfc" module. The scripts in "examples/" update sys.path when called from within the "examples/" directory ( with "sys.path.insert(1, os.path.split(sys.path[0])[0])" ) so that they work from either the install dir or examples subdir.

Revision history for this message
Florian (7-kontakt) said :

Hi Stephen,

I am also struggling with a problem that seems to be import related:
I am using nfcpy (actually only the "nfc" part) as a dependency for an extension in mopidy (http://mopidy.org). I can "import nfc" into this extension, but already the next import from within nfc/clf/__init__.py breaks:

  File "/home/pi/mopidy-nfcread/mopidy_nfcread/frontend.py", line 8, in <module> from .readnfctag import ReadTag
  File "/home/pi/mopidy-nfcread/mopidy_nfcread/readnfctag.py", line 2, in <module> import nfc
  File "/home/pi/mopidy-nfcread/mopidy_nfcread/nfc/__init__.py", line 28, in <module> from clf import ContactlessFrontend
  File "/home/pi/mopidy-nfcread/mopidy_nfcread/nfc/clf/__init__.py", line 32, in <module> import nfc.tag
  ImportError: No module named nfc.tag

Directory structure.

|--- frontend.py (imports readnfc.py)
|--- readnfc.py (imports nfc)
|--- nfc
| |--- __init__.py
| |--- clf (imports nfc.tag) <–– fails here
| | |--- _init__.py
| | |--- etc.
| |--- tag
| | |--- __init__.py
| | |--- etc.
| |--- etc.

I believe I might need relative imports: "from .. import tag" instead of "import nfc.tag" – unfortunately that would mean a lot of code needs to be rewritten as the methods are currently called via their modules e.g. "nfc.tag.activate()".

Do you have any suggestions or ideas how to overcome this problem? Thank you very much.

Revision history for this message
Stephen Tiedemann (stephen-tiedemann) said :

Hi Florian,

I think you have the following options:

a) Install nfcpy into one of the Python module directories (use sys.path to see where they are)
b) Add the nfcpy directory to the module search path with the environment variable PYTHONPATH
c) Add the nfcpy directory to sys.path during runtime (this is what nfcpy examples are doing)


Revision history for this message
Florian (7-kontakt) said :

Hey Stephen,

I opted for option C (changing sys.path). This seems to be working. Only problem currently is that it does not work when the main application is run as a service on system start via init script. I am getting log messages that the reader is ready, but I never receive tags. No error, just does not seem to recognize tags at all. Running the application as user via command line works. This is probably not directly NFCPY related but a problem of mopidy, am I right?

Happy Halloween,