0

I am developing web application through Django/Python framework on Raspberry Pi platform running Debian Linux and Python 2.7.9 .

I have to use multiple threads in Python script to handle multiple peripherals such as camera and microphone. These peripherals must be handled in real time.

I wonder why I can issue "import thread" but not "import threading" in my Python script (does not matter whether it is Python version 2.x or 3.x) ?

pi@raspberrypi:~ $ python --version
Python 2.7.9

pi@raspberrypi:~ $ python ./mythread.py
Traceback (most recent call last):
  File "./mythread.py", line 4, in <module>
    from threading import Thread
  File "/home/pi/threading.py", line 8, in <module>
    del _sys.modules[__name__]
AttributeError: 'module' object has no attribute 'Thread'

pi@raspberrypi:~ $ python3 ./mythread.py
Traceback (most recent call last):
  File "./mythread.py", line 4, in <module>
    from threading import Thread
ImportError: bad magic number in 'threading': b'\x03\xf3\r\n'
dev03
  • 3
  • 2
  • Have you checked [this answer](http://stackoverflow.com/questions/514371/whats-the-bad-magic-number-error) already? It could be a problem due to `*.pyc` files – Alberto Re May 12 '17 at 07:50
  • Removed all *.pyc files under user pi directory and it works, thank you very much in helping to resolve such trivial issue. – dev03 May 12 '17 at 08:48

1 Answers1

0

A couple of issues could exist, one of which, as @Alberto mentioned, is the fact that Python may be trying to used a pre-compiled, byte-code .pyc file. To avoid this, you can remove any pycache files from the related directory, which will then be recompiled when the interpreter tries to run them. The Bash code below will recursively remove .pyc file recursively from the current directory.

find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf