【问题标题】:How can I fix the ImportError: No module named watchdog.observers?如何修复 ImportError:没有名为 watchdog.observers 的模块?
【发布时间】:2020-04-28 08:16:39
【问题描述】:

我想在我的 mac 上使用 python 中的看门狗模块。 我已经通过 Homebrew 下载了 python3。 ($ which python3 导致/usr/local/bin/python3$ which python 导致/usr/bin/python。) 在我安装了 homebrew、python3 并自动 pip3(因为 homebrew)之后,我输入了命令$ pip3 install watchdog。看门狗的安装工作正常,当我输入$ pip3 list 时,看门狗显示为已安装的包。另一方面,当我执行以下脚本时,我收到一条错误消息,告诉我,未找到 watchdog.observer。

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
# pip install watchdog for these packages to work

import os
import json
import time

class MyHandler(FileSystemEventHandler):
    def on_modified(self, event):
        for filename in os.listdir(folder_to_track):
            src = folder_to_track + "/" + filename
            new_destination = folder_destination + "/" + filename
            os.rename(src, new_destination)

folder_to_track = "/Users/eve/Desktop/folder1"
folder_destinatiom = "/Users/eve/Desktop/folder2"
event_handler = MyHandler()
observer = Observer()
observer.schedule(event_handler, folder_to_track, recursive=True)

try:
    while True:
        time.sleep(10)
except KeyboardInterrupt:
    observer.stop()
observer.join()

错误:

Traceback (most recent call last):
  File "/Users/eve/Desktop/auto_ideas.py", line 1, in <module>
    from watchdog.observers import Observer
ImportError: No module named watchdog.observers

在我的编程环境 VS Code 中,我可以使用 python 2.6.9. 64Bit, saved at /usr/bin/python2.6python 2.7.10 64Bit, saved at /usr/bin/pythonpython 3.7.6 64Bit, saved at /usr/local/bin/python3 或最后使用 python 3.7.6 64Bit, saved at /usr/local/opt/python/bin/python3.7 来执行脚本。这些选项都不适合我-每次尝试时都会遇到相同的错误。有人知道,我怎样才能让看门狗在这些条件下工作?在此先感谢

【问题讨论】:

  • 脚本也不叫 watchdog.py。它被称为auto_ideas.py
  • ls $(python -c "import watchdog; print(watchdog.__path__[0])") 看看有没有observers/__init__.py
  • 当我将它粘贴到终端并执行它时,我得到以下输出:Traceback (most recent call last): File "&lt;string&gt;", line 1, in &lt;module&gt; ImportError: No module named watchdog Applications Library Public Movies Sites Desktop Music Documents OneDrive Downloads Pictures - 似乎看门狗本身的导入无法正常工作,而不仅仅是 watchdog.observer。跨度>
  • pip3 show --files watchdog
  • 好的,总结输出如下:Name: watchdog; Version: 0.8.2; Summary: Filesystem events monitoring; Location: /usr/local/lib/python3.7/site-packages; Files: (there are way more) watchdog/events.py, watchdog/observers/__init__.py, ...;

标签: python python-3.x macos import pip


【解决方案1】:

同样的问题!我不知道为什么,但是使用 python.exe 路径运行我的脚本是有效的!

C:\Python37\python.exe c:\rw_projects\mtl_error_notifier\mtl_error_notifier.py

【讨论】:

    猜你喜欢
    • 2019-01-23
    • 2015-02-10
    • 2016-11-18
    • 2020-01-26
    • 2015-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    相关资源
    最近更新 更多