Hanging script and cmd won't close - python

I am trying to run a python script via cmd prompt on my work PC (Windows 7, Python 2.7). The script requires filepaths from different drives on my PC. I am correctly pulling all necessary filepaths and I press Enter to run the script but the script just hangs. The only thing that shows is a blinking underscore. I try to click the X to close the prompt but nothing happens.
I am not able to Ctrl+C out of the program either. I open up Task Manager and I am not able to End Task (nothing happens) or End Process (cmd.exe doesn't even show up in this tab). I also tried Start-->Run-->taskkill /im cmd.exe but nothing happens. The rest of my team has no problem with Python 2.7. The only way to get out of the frozen cmd is to hold down the power button. I do not want to have to keep going through this process especially since this is during work. I'm hoping someone will be able to help me out:
Any idea what's wrong with the version of Python I am using?
How am I able to kill cmd.exe so that I can continue normal work functions without having to hold down the power button and waiting 5-10 minutes to reboot my PC?

The filepaths I was pulling files from were through ClearCase directories. It turned out that I simply had to reinstall ClearCase. There must have been some configuration issue that was causing the cmd to hang and thus forcing a hard reboot. It's good to point out that I am no longer experiencing this problem and that nothing was wrong with the python scripts.

Related

How to make sure that python script will run forever on windows?

I wrote a python script that run an infinite loop and every half second checks if there are new files in a directory:
while True:
files = os.listdir(path_to_dir)
# do something
time.sleep(0.5)
The code runs on windows 10 in a cmd window and I need to make sure it will never stop.
A) I need to find a mechanism (or few mechanisms) that will restart the script in all possible scenarios that it might turn off (it is ok if the restart will happen only 2 minutes later...):
if the computer is restarting
if someone close the cmd windows
if the script end unexpectedly because of unhandle exception or memory leak (it is not suppose to happen...)
B) I want that once in a week, proactively,the script will be turn off and restart.
Any ideas?
Thanks!!
p.s. my first idea was that the python script will only check for new file, and the task scheduled will run it every second, but the minimum interval for task scheduler is 1 minute.
I think these answer all your questions:
Since your script runs on windows, you can use the Microsoft Task Scheduler (which is installed with Windows) to start your Python script when your computer starts up.
If you do not use the cmd window, you can change your Python script extention from .py to .pyw to run the script without a terminal window. A bit more on that here: Executing scripts.
For opening the script after an exception has happend, have a look at this blog post: How to Restart Python Script after Exception and Run it Forever.
To restart your script once a week, you can also use the Task Scheduler mentioned in answer 1. I think this post could help you with restarting your script: Start and stop a python task

Python Shell memory management for always running script

I have written a python (python version is 3) script that runs 24/7. The way I run my script in my Windows machine is the following. I right click on the .py file, then click on "Edit with IDLE" and then "Run". The script has no issue but, due to the many instructions printed in the python shell (I use a logger), after a couple of days this python shell gets very heavy. My newbie question is the following. Is there to limit the number of rows temporarily saved in the python shell to a specific number? Or perhaps somebody has a better suggestion to run this constantly running script that prints a lot of the script steps in the shell? Please, notice how I'm not asking how to run a script 24/7, it's my understanding the best way to do it is though a VPS. My problem is that the data saved in the displayed python shell gets bigger and bigger every day, so I only wonder how to limit the data temporarily displayed/saved in it. Thanks

Python.exe opens in a new console window

I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome

Run Python script as an exe without showing a command window

I am a security student first starting out with Python. I've built my first program for my class and it is meant to be an exe so that it may run on any computer without having me install python onto it. The program is meant to go unnoticed by the user, but whenever it is executed a command windows pops up.
Does anyone know how to make a .py into a .exe that when launched would not bring up the command prompt?
I have already tried compiling a .pyw into a .exe and it still pops up the command prompt.
Googling around the py2exe web site, you need to say setup(windows=['myapp.pyw']) instead of setup(console=['myapp.py']). I have problems finding out which page is saying that exactly, but hints are given here for example (search for "console"):
http://www.py2exe.org/index.cgi/FAQ

Windows executeable created with Py2exe won't run on another computer

I created an executeable of my Python script. Everything works just fine on my first computer but when I try to run this very same executeable in another computer, it wont work. When I try to run my executeable I see that computer tries to open it, because command prompt window appears but nothing else happens, prompt window will disappear. Problem can't be in computer, because both my computers are 32 bit Windows machines and I have done similar things before but this kind of error I see first time.
I don't want to post my code at first, because it is more than 500 lines long. But I will give some more specifications about my problem.
This program I create is actually advanced version of application i made earlier. And earlier version worked fine in both of my computers. This advanced version, I am working on now, is developed into many standalone Python scripts unlike first one which was only one script. Could this be the source of my problem? Should I do something different when running setup.py now when I have many scripts?
And then there is third version of my application. Executeable of it runs fine on computer where I created it, but in my second computer it wont run either. But this time I get error too. When I run this program in my second computer the opening screen of aplication appears, there are some buttons which are meant to open other scripts, when they are clicked on. But when I click them I get following error: The system cannot find the path specified.
If someone is willing to look into my long code, then I am willing to share it. But again I dont want to post it here.
I am using Python 2.7 and windows 7 in my first computer and windows XP on my second computer.
I would be very grateful if someone points me right direction which helps me to solve my problem.

Categories