简体   繁体   English

如何修复 ImportError:没有名为 watchdog.observers 的模块?

[英]How can I fix the ImportError: No module named watchdog.observers?

I want to use the watchdog module in python on my mac.我想在我的 mac 上使用 python 中的看门狗模块。 I already downloaded python3 via Homebrew.我已经通过 Homebrew 下载了 python3。 ( $ which python3 results in /usr/local/bin/python3 ; $ which python results in /usr/bin/python . ) After I installed homebrew, python3 and automatically pip3(because of homebrew), I typed in the command $ pip3 install watchdog . $ which python3结果为/usr/local/bin/python3$ which python结果为/usr/bin/python 。)在我安装自制软件后,python3 和自动 pip3(因为自制软件),我输入了命令$ pip3 install watchdog The installation of watchdog worked fine and when I type in $ pip3 list , watchdog is shown as an installed package. When I execute the following script on the other hand, I get an error which tells me, that watchdog.observer was not found. watchdog 的安装工作正常,当我输入$ pip3 list时,watchdog 显示为已安装 package。另一方面,当我执行以下脚本时,我收到一条错误消息,告诉我找不到 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()

Error:错误:

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

In my programming environment, VS Code, I can execute the script either with python 2.6.9. 64Bit, saved at /usr/bin/python2.6在我的编程环境 VS Code 中,我可以使用python 2.6.9. 64Bit, saved at /usr/bin/python2.6 python 2.6.9. 64Bit, saved at /usr/bin/python2.6 or with python 2.7.10 64Bit, saved at /usr/bin/python or with python 3.7.6 64Bit, saved at /usr/local/bin/python3 or lastly with python 3.7.6 64Bit, saved at /usr/local/opt/python/bin/python3.7 . python 2.6.9. 64Bit, saved at /usr/bin/python2.6或与python 2.7.10 64Bit, saved at /usr/bin/python或与python 3.7.6 64Bit, saved at /usr/local/bin/python3或最后与python 3.7.6 64Bit, saved at /usr/local/opt/python/bin/python3.7 Non of those options work for me - I get the same error, everytime I try.这些选项都不适合我——我每次尝试都会遇到同样的错误。 Does someone know, how I could make watchdogs work under these conditions?有人知道,我怎样才能让看门狗在这些条件下工作? Thanks in advance提前致谢

Same problem!同样的问题! I don't know why but running my script with python.exe path is working!我不知道为什么,但是使用 python.exe 路径运行我的脚本是有效的!

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

I had the same problem in VS Code make sure you Manage Workspace Trust.我在 VS Code 中遇到了同样的问题,确保你管理工作区信任。

Hope this helped希望这有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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