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

7
  • does your script work fine when you run it with python.exe? Commented Apr 1, 2018 at 8: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. Commented Apr 1, 2018 at 8: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. Commented Apr 1, 2018 at 8: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 Commented Apr 1, 2018 at 9: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: kb.iu.edu/d/afnz Commented Apr 1, 2018 at 9:10

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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?
You need to choose window-based to be a background process.
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 .
I will record my program and publish here
|
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.

Comments

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

6 Comments

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...
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?
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
It’s ok :( , Thanks anyway
Hmmm... Just found out a way. Try using pyinstaller. Use the --noconsole option, like this: pyinstaller --noconsole scriptName.py
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.