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
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.
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.
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
Related
Here's the code:
import emoji
import time
print("\U0001F600")
time.sleep(50)
Here's the output when I run in the python idle shell thingo:
Here's the output when I run the program in windows terminal thingo:
I've got this game thing I'm working on for an assignment, and I've simply just copy pasted emojis into the print function, and when I run it in PyCharm, it works just like in idle shell. But when I turn it into an executable using the pyinstaller module thing, it just shows ? instead of the emoji.
By the way, I'm not that advanced in this kinda thing, so I'd appreciate if someone could explain how to fix this problem :)
This does not work in the normal cmd console. If so, you need the
Terminal
(In addition, you do not need the emoji module for your method)
I've recently started programming in python for my job, so I'm quite new to it. My goal is to create a graphic interface so that the user can run a program that I have been developing in R. The interface is done using the Tkinter module from python (version 3.3).
The problem comes when I have to call the R interpreter from python to run an R file that is generated (run.R file). The curious thing is that this only happens when I try to run my script in Windows, not in Linux. In both cases, I am trying to use the os module from python.
This is the code that is not working for Windows:
os.chdir(outRW) #first I change the working directory to the one where the run.R file is
os.system("C:\R-3.6.1\bin\Rscript run.R")
When I execute this, it changes the directory successfully, but when it comes to calling the R interpreter, it shows me this error:
The filename, directory name, or volume label syntax is incorrect.
However, I have tried running the "C:\R-3.6.1\bin\Rscript run.R" command in the Windows Command Prompt and it works perfectly.
I have also tried adding the path to R to the environmental variables, but again I could only make it work in the Command Prompt, not with python.
I guess there is something very obvious that I am missing here, but I cannot see it.
Any help or comments are very much appreciated!
Thank you!
Use double backslashes.
In R you need to use double backslashes \\, otherwise it'll try to interpret it as an Escape Character.
Use this and it will work:
os.system("C:\\R-3.6.1\\bin\\Rscript run.R")
Hope this helps.
I have a simple script I wrote, and when trying to run it (F5) , I get this msg:
================== RESTART: C:\Users\***\Desktop\tst.py ==================
I restarted the shell, reopened the script, but still, the same msg appears.
I use python 3.5.1 and I tried to simplify the script as much as possible, but I still get this result. Now my script is only one line with a simple print(1) command and I still get this msg.
Was there something wrong with the shell installation?
I have a simple script I wrote, and when trying to run it (F5)
That's the hotkey for IDLE to run a file. It is not ordering to do anything. It's a log statement to explicitly declare that your namespace is being cleared and the file is going to be ran fresh again.
no, I didn't tell it to restart
But you did... You pressed F5
The same thing is happening with my shell. In the older versions, this does not happen. I've also noticed that when I press Python 3.5.2 Module Docs, I have my Internet browser open up and I see my directory being displayed onscreen. It looks like:
C:\Users\mycomputername\AppData\Local\Programs\Python\Python35-32\DLLs.
Is that suppose to happen? Is that secured? I don't know.
I've also found that this prints out whenever I "imported" something. So if I use an import command, and put it right before the line of my random name, it will print out that "RESTART" thing. It's always at the beginning. Or what it reads as the beginning.
CIsForCookies, my guess is that you don't actually have a complete script; maybe you have just a function definition and you haven't included a line to run that function. (I had this problem and then remembered to call the function I defined; the problem went away.)
You may have made the same mistake as me and ran a program and then you wonder why RESTART is all that shows up. My program was working perfectly I just did not print anything or ask for any input so it ran and was done with the program and nothing showed up.
I am facing problems running a Rscript via Python using os.system() or subprocess().
Using os.system() to run commands via python works generally fine for me (e.g. with gdalwarp.exe) but not with Rscript.exe.
The only difference I can see are spaces in the path.
Avoiding problems with spaces in the path are easy overcome in the CMD-window by putting the paths in quotation marks.
Executing the following command is successfull.
"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"
But I am stuck with Python.
What I tried so far with python:
os.system("C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R")
os.system(r'"C:/Program Files/R/R-3.0.2/bin/Rscript.exe" "D:/.../otsu_Script.R"')
subprocess.call([r'C:/Program Files/R/R-3.0.2/bin/Rscript.exe', r'D:/.../otsu_Script.R'])
Does anybody see what I am doing wrong?
Thanks in advance,
Eike
After getting mental on such a simple problem. I decided to reinstall RStatistics to a path with no spaces or points, like:
C:/R/bin/Rscript.exe.
Now
subprocess.call(["C:/R/bin/Rscript.exe", "D:/otsu_Script.R"] ) or
os.system("C:/R/bin/Rscript.exe D:/otsu_Script.R") are working just fine.
Should have tried it two days ago...
... but now I am a happy monkey anyway :-)
It probably is way too late now and I have seen you solved the issue, but I was having a similar issue (although in a Linux system) and it might help someone else now; this command was not working when called inside python although it worked directly on the terminal/command-line.
os.system("R CMD BATCH ./read_lengths_batch.R")
I tried many solutions, including subprocess and others but found it to be easier than that. In my case, and I understand it might be different in Windows, I just had to add a & at the end of the call for it to run in the background. Somehow it seemed R would shut down with the Python script instead of doing its work.
os.system("R CMD BATCH ./read_lengths_batch.R &")
Strangely, it was also working if in my folder I would have the same file copied with a .txt extension: read_lengths_batch.R and read_lengths_batch.txt.
Hope it helps someone!
I have a program that opens a lot of folders right now, and I am hoping that I can use the already open explorer instances to open new ones. I have this code:
import subprocess
subprocess.Popen(r'explorer /select,"C:\path\of\folder\file"')
I have seen threads on doing this in C#. Does anyone know a way it can be done in Python?
Thanks,
Ben
I do not believe you can do this. If there is any hope you will probably need to look at Python for .Net, you might give IronPython a try. But the code above using subprocess is simply giving you the same result as if you ran that string in command line. Which launches the explorer application with those arguments. But once its started, its off running on its own. Its not looking for command line input any more. I've looked at the list of command line arguments for explorer, and I see nothing that would indicate you can manipulate it once its running. Sorry, hope this helps.