【问题标题】:import threading not working python 2.7 [closed]导入线程不起作用python 2.7 [关闭]
【发布时间】:2013-09-06 20:41:20
【问题描述】:

我正在尝试在即将到来的项目中使用线程,但我无法让它工作。我已经尝试在http://www.tutorialspoint.com/python/python_multithreading.htm 上运行第二个示例,我想这是工作代码,但我得到了错误...

Traceback (most recent call last):
  File "C:/Python27/threads/threading.py", line 3, in <module>
    import threading2
  File "C:\Python27\lib\site-packages\threading2\__init__.py", line 49, in <module>
    from threading import _active,_DummyThread
  File "C:\Python27\threads\threading.py", line 8, in <module>
    class myThread (threading2.Thread):
AttributeError: 'module' object has no attribute 'Thread'

怎么了?似乎没有名为 threading 的模块。我到处找它。有谁知道我在哪里可以找到这个模块以及如何安装它?

【问题讨论】:

  • 附带说明,我不确定将 Python 2.4 称为“新”的教程是否是最好的起点……
  • 您是否将自己的代码放入C:\Python27 中?这不是一个好主意。
  • threading2?以前从未听说过。
  • @user2357112:快速 PyPI 搜索 turns it up。显然是更厉害了。 :)
  • 我不知道为什么在标准库threading 模块中会有任何对它的引用。看起来可能存在某种循环导入错误。编辑:那不是标准库threading 模块。将代码放在 Python 安装文件夹中并将其命名为与标准库模块相同的名称都是不好的想法。

标签: python multithreading python-2.7 installation


【解决方案1】:

您调用了自己的文件threading,现在您正在导入自己的脚本,这不是您想要的。请不要将现有包/模块的名称用于您自己的脚本。

threading2.py 中的代码尝试import threading 时,Python 首先查找具有该名称的已导入模块。如果失败,它会在sys.path 中的每个位置查找它可以加载的名为threading.py 的文件。通常,它会在标准库中找到那个。但是,如果你有一个名为 threading.py 的文件与脚本位于同一位置——或者如果它是脚本本身的名称——Python 会首先找到那个文件,然后加载它。

另外,不要将自己的脚本保存在安装 Python 的文件夹中。

【讨论】:

  • 这可能是正确的答案,但你需要解释一下……
  • 哦,我错过了循环导入。感谢您提供更多信息。
  • 是的,谢谢你,我可能应该接受那个好的答案
猜你喜欢
  • 1970-01-01
  • 2021-04-01
  • 2022-11-21
  • 1970-01-01
  • 1970-01-01
  • 2016-10-28
  • 1970-01-01
  • 2014-09-01
  • 1970-01-01
相关资源
最近更新 更多