Running a Python Script in the background from Startup [duplicate] - python

This question already has an answer here:
Python script start on boot
(1 answer)
Closed 3 years ago.
I want to monitor a file in a specific location and check it for updates, which upon being updated launches certain actions via a python script.
Ideally, this program would always be running in the background and would automatically start with the computer so that the user does not have to mess with starting/stopping it.
I have researched the topic some and have found daemon and similar tools, but I do not understand how they work and they look far more complex then what I need. Also, many of the examples that I am looking at use Ubuntu OS and I will be using Windows.
Are there any python modules that exist to do this already or any direction you can point me to get started? I apologize if this question has been answered already, however I have not found it in my research.
I plan on editing this post to include code that performs this action, however I currently do not know where to start.

On Windows you could create a batch script and schedule it using the Windows Task Scheduler to run as a process on boot.
You can trigger the Python script to run inside the batch file,
python file.py
Alternatively, if you're using something like Anaconda as your environment manager; you could write a batch file to activate Anaconda using the activate.bat contained in the Scripts folder of your Anaconda installation path and then follow the usual calling steps.

Related

Run Python-Script anywhere on Windows [duplicate]

This question already has answers here:
How to run a Python script portably without specifying its full path
(4 answers)
Closed 3 years ago.
I am looking for a way to run a python script from anywhere.
I have seen this solution but as it is based on Linux I am looking for a way on Windows (10).
Basically what I want to do is, execute a script with a given parameter. Because it could be disturbing for the user, I am using a .pyw-file extension to hide the console.
Things that came to my mind:
a PATH-Variable
PowerShell-Script
Batch-file
Sadly I am not familiar/experienced with neither of those, so I can't really tell if these ideas even provide a way to do that.
Any answer is appreciated.
Edit: I would like to make the command as short as possible for the client so it is not necessary to write a novel to exec one simple task.
Another Edit:
I want the user/client to open its cmd/ps and use a command which executes my python-script, which is not in this directory he is, when opening the cmd. So the script is somewhere on his computer, but shall be executable by command from anywhere.
try to create a "code.bat" file with this command "python script.py" inside your .bat file then add the "code.bat" to your path in your environments anytime you want to run the script just type code in your shell.

Running Python through Powershell or Git Bash

I will preface this with, I know questions like this have been asked, but I feel as I can't find my answer.
My question is this: I downloaded Python about 2 months ago to begin learning it. I just recently starting trying to use the terminal more and would like to move forward using it a lot for commits and package installs.
Unfortunately I am having a very hard time getting Python to run properly through either Git Bash or Windows Powershell. I think i may have moved files around too much before even starting with using the terminal and now I'm not sure what's correct.
Should I uninstall and re-install?
I am on Windows 10, any advice onhow to organize my files properly so everything can run correctly. I would also like to be able to open my text editor from the terminal, PyCharm or Atom.
Thanks in advance!!
Try first to simplified your PATH (as I did here) in a CMD session.
set PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
set GIT_HOME=C:\Path\to\Git
set PATH=%GIT_HOME%;%GIT_HOME%\bin;%GIT_HOME%\usr\bin;%PATH%
SET PATH=C:\Users\Aerovistae\AppData\Local\Programs\Python\Python36-32;%PATH%
Once you have the right path, you can replicate it in your user environment variables.
(replace C:\Users\Aerovistae\AppData\Local\Programs\Python\Python36-32 by your own Python2 installation path)
Then type bash and check python does work.

Create python script that runs at startup [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Background
I need to create a python script that runs at start-up. The problem is that this script must be platform independent because it will be used on different operating systems. It needs to be an automatic set up because it will be run by the user and so I won't be able to set task schedulers up on each individual machine.
Questions
How to find out which OS a computer is running on in Python?
How to make a script run at startup (Linux, Mac OSX, Windows)
Question 1 is easy:
How to find out which OS a computer is running on in Python?
That's sys.platform:
if sys.platform.startswith('win') or sys.platform.startswith('cygwin'):
do_windows_stuff()
elif sys.platform.startswith('darwin'):
do_osx_stuff()
elif sys.platform.startswith('linux'):
do_linux_stuff()
else:
raise Exception("Nobody's written the stuff for {}, sorry".format(sys.platform))
The second part is also easy, but not in the way you wanted to hear:
How to make a script run at startup (Linux, Mac OSX, Windows)
You don't. Not from within the script. You use some kind of installer (or package postflight script, or whatever).
Adding things that run at startup requires root/admin rights. Your script (hopefully) is not running with such rights. Therefore, it can't do it. Yes, it's possible to elevate privileges in various ways, but that almost certainly isn't what you want to do inside a script that's going to end up running at startup.
So, how does your installer do it then?
OS X: You need to create a Launch Daemon, with an accompanying launchd plist. This is described in Creating Launch Daemons and Agents. You shouldn't be trying to do this if you haven't read that article, and you'll already know how if you have read that article, so there's not much else to say.
Windows: The official way to do this is explained in Run and RunOnce Registry Keys. Again, you shouldn't do this without reading this article, and after reading the article it's pretty obvious, except for two things: First, out of the four keys, it's the HKLM Run key. Second, in modern Windows, this doesn't actually run at startup, but at the first login after startup; if that's not acceptable, look into RunServices instead.
Linux: What's an installer? And were you expecting one way to do it for every distro family? This primer gives you most of the information you need, except for knowing exactly what you want to do on each distro. In general, if you just want your script to run once and quit, and there's an rc.local.d, and you just need to drop a link in there. Otherwise, you either need to create an rc.d script, install it to the right place, and run the right chkconfig command, or you need to edit rc.local to run your script. But the simplest thing is: just put English text in the INSTALL file telling people to do it. Eventually, when someone decides to make a DEB for Ubuntu or an RPM for Redhat or whatever, they'll do the right thing for their distro, and either submit a patch to you or maintain it separately.

Creating a Scheduled task in Tkinter on Unix

Through an application I have made with Tkinter, I'm trying to add a command to run a script every week. When the program is closed the command should be in forever place.
I've sifted through the documentation on cron, but there doesn't seem to be a way to edit the crontab without using the shell. Also I've looked through the 'at' command, but that only seems to run once.
My question is - How can one create a weekly recurring task by issuing a single command in Python on Unix?
If not with only 1 command, can I use multiple?
In most modern Linux distros like Debian or Ubuntu, you can add an executable file (like a shell script or a symlink to one) into /etc/cron.weekly and it will be automatically run once a week for you. This is using the anacron command, which is fairly common these days.

Issue with cx-freeze application: It is running for only 1 second [duplicate]

This question already has answers here:
Pygame and cx_freeze: segmentation fault
(5 answers)
Closed last month.
I made a game in python, and then exported it with cx-freeze. For some reason, when I try to double-click on the application it opens a command line for about a second and then closes. However, when I run it myself with just the python IDLE it works fine. What am I missing?
If it helps: I have graphic files in a separate folder called data and I'm using the normal python modules + pygame.
If you application is a GUI, you should freeze it with the "Win32GUI" base, so it doesn't open a command line. Have a look at an example in the docs.
I don't know why it's closing again - maybe it's failed to include something it needs. When you freeze it with the GUI base, any error messages should appear in a message box, which might help you work out what the problem is.

Categories