How to execute Python scripts in Windows?
When you execute a script without typing "python" in front, you need to know two things about how Windows invokes the program. First is to find out what kind of file Windows thinks it is:
C:\>assoc .py
.py=Python.File
Next, you need to know how Windows is executing things with that extension. It's associated with the file type "Python.File", so this command shows what it will be doing:
C:\>ftype Python.File
Python.File="c:\python26\python.exe" "%1" %*
So on my machine, when I type "blah.py foo", it will execute this exact command, with no difference in results than if I had typed the full thing myself:
"c:\python26\python.exe" "blah.py" foo
If you type the same thing, including the quotation marks, then you'll get results identical to when you just type "blah.py foo". Now you're in a position to figure out the rest of your problem for yourself.
(Or post more helpful information in your question, like actual cut-and-paste copies of what you see in the console. Note that people who do that type of thing get their questions voted up, and they get reputation points, and more people are likely to help them with good answers.)
Brought In From Comments:
Even if assoc and ftype display the correct information, it may happen that the arguments are stripped off. What may help in that case is directly fixing the relevant registry keys for Python. Set the
HKEY_CLASSES_ROOT\Applications\python26.exe\shell\open\command
key to:
"C:\Python26\python26.exe" "%1" %*
Likely, previously, %*
was missing. Similarly, set
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
to the same value. See http://eli.thegreenplace.net/2010/12/14/problem-passing-arguments-to-python-scripts-on-windows/
HKEY_CLASSES_ROOT\Applications\python.exe\shell\open\command
The registry path may vary, use python26.exe
or python.exe
or whichever is already in the registry.
HKEY_CLASSES_ROOT\py_auto_file\shell\open\command
How to constantly run Python script in the background on Windows?
On Windows, you can use pythonw.exe
in order to run a python script as a background process:
Python scripts (files with the extension
.py
) will be executed bypython.exe
by default. This executable opens a terminal, which stays
open even if the program uses a GUI. If you do not want this to
happen, use the extension.pyw
which will cause the script to be
executed bypythonw.exe
by default (both executables are located in
the top-level of your Python installation directory). This suppresses
the terminal window on startup.
For example,
C:\ThanosDodd\Python3.6\pythonw.exe C:\\Python\Scripts\moveDLs.py
In order to make your script run continuously, you can use sched
for event scheduling:
The sched module defines a class which implements a general purpose
event scheduler
import sched
import time
event_schedule = sched.scheduler(time.time, time.sleep)
def do_something():
print("Hello, World!")
event_schedule.enter(30, 1, do_something, (sc,))
event_schedule.enter(30, 1, do_something, (s,))
event_schedule.run()
Now in order to kill a background process on Windows, you simply need to run:
taskkill /pid processId /f
Where processId
is the ID of the process you want to kill.
Running python scripts on windows 10 cmd just by typing python nameofthescript.py
dpath
is the equivalent to path
for data files. Type dpath /?
note it was once called append
and the help hasn't been updated.
That answers your question as you've asked.
Perhaps you should have asked how do I just type my script name?
So
associate .py
with the python interpreter.
Use commands ftype
and assoc
ftype PythonScript=c:\pathtofolder\python.exe %*
assoc .py=PythonScript
Then add your SCRIPT path to the path command
Setx path "%path%;C:\MyPythonScriptFolder"
Set path=%path%;C:\MyPythonScriptFolder"
Then tell windows command prompt to assume .py files are executable so it will search the path for it.
Set pathext=%pathext%;.py
Run python script on Windows 10
Your code is working good.
The problem is that the cmd closes the window too fast and you can't see the result.
Just add a command to wait for your interaction before closing the window.
You can add this at the end of your code:
input("Press Enter to finish...")
Or pause the execution after completion:
import time
[at the end of the code pause for 5 seconds....]
time.sleep(5)
Related Topics
Get Ip Address of Visitors Using Flask for Python
Where Is Python's Sys.Path Initialized From
Printing All Instances of a Class
How to Fix Overlapping Annotations/Text
Pythonic Way to Check If a List Is Sorted or Not
Dictionary: Get List of Values for List of Keys
Skip the Headers When Editing a CSV File Using Python
Python Append() VS. + Operator on Lists, Why Do These Give Different Results
How to Convert String to Binary
Slicing a List in Python Without Generating a Copy
Why Do My Tkinter Widgets Get Stored as None
How to Compute the Similarity Between Two Text Documents
Print Multiple Arguments in Python
Syntax Behind Sorted(Key=Lambda: ...)