簡體   English   中英

使用 if __name__ == '__main__' 時代碼不運行

[英]code doesnt run when using if __name__ == '__main__'

在 sublime、pycharm 或 spyder 中運行任何代碼時,我總是收到此消息並且代碼停止。 我目前正在使用的代碼: https://imgur.com/a/fNaUPu2

RuntimeError:在當前進程完成其引導階段之前,已嘗試啟動新進程。

    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 == ' main ' 時:在代碼前面它崩潰: https://imgur.com/a/YyUtWDS

但是當我在 python cmd 中運行相同的代碼時,它工作得很好: https://imgur.com/a/ZYPUrJY

如果有幫助,我有 5 5600g apu 和 python 3.10

任何幫助將不勝感激,在此先感謝

[編輯]我看了你的視頻,你的代碼沒有崩潰,這個過程就結束了。 您只需添加睡眠即可查看瀏覽器的工作情況。

這對我有用:

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移到頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM