简体   繁体   中英

code doesnt run when using if __name__ == '__main__'

When running any code in sublime, pycharm or spyder I always get this message and the code stops. Code I'm currently using: https://imgur.com/a/fNaUPu2

RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase.

    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..

When adding if name == ' main ': in front of the code it crashes: https://imgur.com/a/YyUtWDS

But when I run the same code in python cmd it works just fine: https://imgur.com/a/ZYPUrJY

I have a 5 5600g apu and python 3.10 if that helps

Any help would be greatly appreciated, Thanks in advance

[Edit]I saw your video and your code doesn't crash, the process just ends. You can just add sleep to see the browser work.

This worked for me:

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)

Maybe you can omit freeze_support() in your case.

And as someone suggests, I would move import undetected_chromedriver as uc to the top, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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