简体   繁体   English

如何在后台/作为进程运行python脚本?

[英]How to run python script on background/as a process?

I want to run a python script as a process/in the background. 我想在后台运行python脚本作为进程。 I searched and found pythonw.exe but when I do pythonw.exe name_of_my_script.py nothing happened. 我搜索并找到pythonw.exe但是当我执行pythonw.exe name_of_my_script.py什么都没发生。 Am I doing something wrong and/or how else would I run it in the background? 我做错什么了吗?和/或我还会在后台运行它吗?

This is part of my code that do problem : 这是我做问题的代码的一部分:

from selenium import webdriver

I will thank you for any help whatsoever. 我将感谢您的任何帮助。

note: With the help of the comments here, I understood that also python.exe running don't work and bring error - NameError: name 'PROTOCOL_TLS' is not defined ( even tough in normal running it doesn't have errors. 注意:借助此处的注释,我了解到python.exe的运行也无法正常工作并带来错误-NameError:未定义名称“ PROTOCOL_TLS”(即使在常规运行中很难,也没有错误)。

edit: i used http://pytoexe.com/ and i got exe file that work but the cmd console still exist ( mabye it because i use phantomjs ? ) click here 编辑:我用http://pytoexe.com/和我得到的exe文件工作,但CMD控制台仍然存在(mabye它,因为我使用phantomjs?) 请点击这里

I'd approach this by using threading module, and use os.fork() - but I'm not running Windows. 我会通过使用threading模块并使用os.fork() -但我没有运行Windows。 I suggest having a look through Difference in behavior between os.fork and multiprocessing.Process for other OSes. 我建议浏览一下其他操作系统在os.fork和multiprocessing.Process之间的行为差​​异

By "I want to run a python script as a process/in the background," do you mean make the window not visible? 通过“我想在后台/在后台运行python脚本”,您是说使窗口不可见? if so, just save the script as .pyw instead of .py 如果是这样,只需将脚本另存为.pyw而不是.py

You can create your own exe by going to http://pytoexe.com . 您可以通过转到http://pytoexe.com创建自己的exe。 After that convert your script. 之后,转换您的脚本。 Choose windows-based and convert it. 选择基于Windows并将其转换。 You'll not have any problems after that. 之后,您将没有任何问题。 Hope that helps. 希望能有所帮助。

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

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