0

I want to run a python script as a process/in the background. I searched and found pythonw.exe but when I do pythonw.exe name_of_my_script.py nothing happened. 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.

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

omersk2
  • 79
  • 1
  • 11
  • does your script work fine when you run it with python.exe? – CodeCupboard Apr 01 '18 at 08:54
  • 1
    What do you mean "in the background"? You can just run a python script in a terminal window and leave it running while you perform other tasks in other windows. – Ollie Apr 01 '18 at 08:55
  • In Windows you need to create a Windows service. Look at Hammond & Robinson, *Python Programming on Win32*, Chapter 18, ISBN 978-1-56592-621-9. – BoarGules Apr 01 '18 at 08:57
  • 1
    1.hmm when i run it with python.exe it doing error - NameError: name 'PROTOCOL_TLS' is not defined ( when i run it normally it doesn't have any error) mabye it's the problem? 2. i can leave the window terminal open but i want to run it without anything open - even not the terminal. think about it like a virus - the user don't know he exist , he don't leave after him open programs like terminal. 3. I will try thanks – omersk2 Apr 01 '18 at 09:07
  • What OS do you use? If you are on Linux/*nix, just run it from terminal and append `&` behind the command. For more info: https://kb.iu.edu/d/afnz – Joe Samanek Apr 01 '18 at 09:10
  • I have windows. – omersk2 Apr 01 '18 at 09:16
  • You should edit the Title of this question 'backgroung' to 'background' – NER1808 Apr 01 '18 at 10:15

3 Answers3

1

You can create your own exe by going to http://pytoexe.com . After that convert your script. Choose windows-based and convert it. You'll not have any problems after that. Hope that helps.

Jaguar Ro
  • 26
  • 1
  • Hii, first of all thanks alot, seconde, i did it and it's do what it need to do but the cmd console stays open because i use phantomjs and it opens a cmd console do you know what to do? – omersk2 Apr 01 '18 at 10:50
  • You need to choose window-based to be a background process. – Jaguar Ro Apr 03 '18 at 07:53
  • I choosed window based option - it’s not open the console of the whole script but it opens a console every time i use phantomjs . – omersk2 Apr 03 '18 at 09:10
  • I will record my program and publish here – omersk2 Apr 03 '18 at 09:11
  • [see this video on youtube](https://www.youtube.com/watch?v=lDLWnXWNoQU&feature=youtu.be) – omersk2 Apr 03 '18 at 09:32
  • It doesn't work because the program phantomjs.exe opens a console, not the .exe. If phantomjs.exe is made to open a console, then it will open one. Try using another program. – Jaguar Ro Apr 04 '18 at 12:51
  • Instead of phantomjs? I dont think there is more option :( – omersk2 Apr 04 '18 at 16:49
  • It’s strange because When i run it normally ( not converting to exe ) , the phamtomjs dont open console. – omersk2 Apr 04 '18 at 16:53
0

I'd approach this by using threading module, and use os.fork() - but I'm not running Windows. I suggest having a look through Difference in behavior between os.fork and multiprocessing.Process for other OSes.

James McPherson
  • 2,476
  • 1
  • 12
  • 16
0

By "I want to run a python script as a process/in the background," do you mean make the window not visible? if so, just save the script as .pyw instead of .py

TMagnetB
  • 35
  • 8
  • I tried to do pythonw.exe name_of_my_script.py(which is the same) but it didn't do anything, as i write, i think it because python.exe name_of_my_script.py raise this error PROTOCOL_SSLv23 = PROTOCOL_TLS NameError: name 'PROTOCOL_TLS' is not defined. whereas name_of_my_script.py doesn't bring errors... – omersk2 Apr 01 '18 at 09:54
  • anyway, i tried to save it as .pyw and run in in the terminal but again, it didn't do anything just like pythonw.exe name_of_my_script.py Do you know why when i run it in this way - python.exe name_of_my_script.py it raise error while running it in this way - name_of_my_script.py doesn't raise any error? – omersk2 Apr 01 '18 at 09:57
  • Sorry, I do not know why that happens. I made a keylogger just today and saved it as .pyw and it works just fine. I'm very sorry that I can't help – TMagnetB Apr 01 '18 at 10:08
  • It’s ok :( , Thanks anyway – omersk2 Apr 01 '18 at 10:13
  • Hmmm... Just found out a way. Try using pyinstaller. Use the --noconsole option, like this: pyinstaller --noconsole scriptName.py – TMagnetB Apr 02 '18 at 10:00
  • Hi, i done it buy i don't understand what it should do ... it just write alot of lines in the terminal like - 513 INFO: checking COLLECT - and add one file - nameofthescript.spec – omersk2 Apr 02 '18 at 11:25