简体   繁体   中英

thread vs threading module in Python

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'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM