How to make a python file run in the background when minimized? - python

I made a Python program that I do NOT want to turn into .exe or anything else.
However since the program needs python.exe to run, it runs python.exe openly.
I want to know if I can add a piece of code to the program I made so that it runs in the background.
BTW, I do not want to run through cmd.I want it to be a background process automatically.
Basically, I want to add some code into my program so that as soon as it is clicked upon, it runs in background.

I found out the way to do this easily: I just renamed my file.py into file.pyw
Now, it runs without a console. Basically I typed this in the cmd:
ren file.py file.pyw

Related

Python Executable how to on Windows

I'm running windows
I've got a python script that works as expected when I run it from my interpreter (Anaconda, i think that's an interpreter...), but when I run it from my file manager by double clicking the script ("script.py") I see a quick black screen flash, but nothing else. I'm using the input() function, so that's not the issue.
When I try the same thing with a simple test script, which is just the print() function and the input function, the command line or terminal screen, whatever the black screen is called, displays my printed string and closes only after I press enter.
I believe my issue has to do with file paths and working directories, but I'm a noob and I don't know how to solve this. My script declares this variable:
inv_folder=r"C:\Users\domin\OneDrive\Desktop\Test Folder" #folder to look for completed recipe files
which I believe is an absolute filepath. I then loop through the files in that folder and open and read them
for filename in os.listdir(inv_folder):
Main goal is to send my script to someone else's computer, and allow them to run it simply by double clicking on the file. Trying to do that on my computer and failing
Doubleclicking the .py file in Windows will generally run the file. In your case, if all your script is doing is looking for a directory and looping through files in it, it is quite possible that the script is working correctly, but that it is closing immediately upon completing the script.
E.g. if you have the following python file hello.py with the single line of code.
print("hello world")
If you doubleclick hello.py, you will only see a black window flash for a second, as the program runs and exits.
To check whether your code is working correctly or not, you are best to navigate to that folder in your command prompt and run the program within the command prompt. E.g. typing "hello.py" in the right folder will then show the correct output in the console.
Try running it in the console first, seeing if the code is giving any errors, and then working from there. But the behaviour you are describing is basically standard Windows behaviour and not necessarily an error.
If you need to pause the window after running, please see the following StackOverflow question and answers about "Python Equivalent to System('PAUSE')"
Have you considered to compile it?
You can compile it easily with pyinstaller and this command:
C:\<path_to_python>\Scripts\pyinstaller.exe --onefile -n <your script name> <your script path>
Of course replace the <> with the relevant paths for you.

python 3.6.2 not giving me option to create or run files or anything of the sort

I know some basics of Java and C++, and am looking to learn Python
I am trying to develop some random stuffs to get a good feel of how it works, but i can only make 1 line scripts that run every time i press enter to go to the next line.
I've seen tutorial videos where they can just open up files from a menu and type away until they eventually run the program.
I'm using IDLE, and i don't see options to open up new stuffs; I can only make one or two line programs. When i tried to make a calculator program, i didnt know how to run it because it ran every line of code i typed in unless there were ...'s under the >>>'s.
I think it's because i am in interactive mode, whatever that is.
How do i turn it off, if that's the problem?
There are many different options for writing python scripts. The simplest to use is Idle, it come with the Python download. Within Idle, create a new document to write a script. Once finished, save it as a .py file, and you can run it within Idle. For my personal setup, I use the text editor Atom. I can create documents easily, and run them through the terminal on my computer. Hope this helps.
To exit out of the "interactive mode" that you mentioned (the included REPL shell in IDLE) and write a script, you will have to create a new file by either selecting the option from the top navigation bar or pressing Control-N. As for running the file, there's also that option on the navigation bar; alternatively, you can press F5 to run the program.

How to make the text shell not appear when running a program

I'm just wondering (as im starting to make a game with pygame) is there a way to 1) Run a program that only opens a GUI and not the shell
and
2) Run the program without having to edit with idle (I'd like to do this so the game will look more professional.
I would like to just have a desktop shortcut or something that when clicked, runs only the GUI and doesnt show my code or show the shell.
If you're on windows, a quick thing could be to make a one-line batch script.
start pythonw.exe <filename>
The start keyword causes the shell to not wait for the program to finish.
pythonw vs python prevents the python terminal from appearing.
If you're on linux/mac, you could do the same with shell scripts.

Command window popping up when running a Python executable?

I recently made an executable of a Python program with py2exe, and when I ran the executable, a command window appeared for a split second and then disappeared. My program itself never actually ran at all. Everything is still inside the dist folder, so I'm not sure what's actually wrong. Is there a solution for this?
If all your program does is print something and you run it by double-clicking the executable, then it simply closes the console when it finishes running. If you want the window to stay open, run your program from the command line. You can also create a batch file that runs your program and then pauses the console, so that you at least get a "press any key" before the console closes.

How to run a python program in the background?

How can I make a python program run in the background?
I don't need the console running since all it does is sends me emails with updates once an hour.
Is there a way to do it when I convert it to exe using py2exe?
I am using python 2.7.8.
Also, is there a way to make it open in the boot menu immediately when the computer turns on from the code?
Thank you
If you are asking strictly how to hide the console as part of the py2exe bundling, that's simple. See - Hiding command-line dialog in py2exe
If you are asking how to have a program without a GUI, just build the program in py2exe as above and run it. Unless you have created a GUI, it will run on it's own until it terminates or is terminated.

Categories