如何修复导入错误:没有名为watchdog.observators的模块?

2024-04-18 22:29:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在mac上使用python中的看门狗模块。 我已经通过自制下载了python3。($ which python3导致/usr/local/bin/python3$ which python导致/usr/bin/python。) 在我安装了homebrew、python3和自动pip3(因为homebrew)之后,我输入了命令$ pip3 install watchdog。watchdog的安装工作正常,当我输入$ pip3 list时,watchdog显示为已安装的包。另一方面,当我执行下面的脚本时,我得到一个错误,它告诉我,没有找到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代码中,我可以使用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执行脚本。这些选项中没有一个适合我——我每次尝试都会遇到同样的错误。有人知道,在这种情况下,我如何让看门狗工作吗?提前谢谢


Tags: tofromimportbinusrlocal错误pip3