【问题标题】:code doesnt run when using if __name__ == '__main__'使用 if __name__ == '__main__' 时代码不运行
【发布时间】:2021-12-26 15:21:39
【问题描述】:

在 sublime、pycharm 或 spyder 中运行任何代码时,我总是会收到此消息并且代码会停止。 我目前使用的代码:https://imgur.com/a/fNaUPu2

运行时错误: 已尝试在 当前进程已完成其引导阶段。

    This probably means that you are not using fork to start your
    child processes and you have forgotten to use the proper idiom
    in the main module:

        if __name__ == '__main__':
            freeze_support()
            ...

    The "freeze_support()" line can be omitted if the program
    is not going to be frozen to produce an executable..

当添加 if name == 'ma​​in': 在代码前面它会崩溃:https://imgur.com/a/YyUtWDS

但是当我在 python cmd 中运行相同的代码时,它工作得很好:https://imgur.com/a/ZYPUrJY

如果有帮助,我有一个 5 5600g apu 和 python 3.10

任何帮助将不胜感激, 提前致谢

【问题讨论】:

标签: python sublimetext3


【解决方案1】:

[编辑]我看了你的视频,你的代码没有崩溃,这个过程刚刚结束。您只需添加睡眠即可查看浏览器的工作情况。

这对我有用:

from multiprocessing import freeze_support
from time import sleep

if __name__ == '__main__':
    freeze_support()
    import undetected_chromedriver as uc
    driver = uc.Chrome()
    driver.get('https://nowsecure.nl')
    sleep(10)

也许你可以在你的情况下省略freeze_support()

不过,正如有人建议的那样,我会将import undetected_chromedriver as uc 移到顶部。

【讨论】:

    猜你喜欢
    • 2018-11-22
    相关资源
    最近更新 更多