Drag and drop onto Python script in Windows Explorer - python

I would like to drag and drop my data file onto a Python script and have it process the file and generate output. The Python script accepts the name of the data file as a command-line parameter, but Windows Explorer doesn't allow the script to be a drop target.
Is there some kind of configuration that needs to be done somewhere for this work?

Sure. From a mindless technology article called "Make Python Scripts Droppable in Windows", you can add a drop handler by adding a registry key:
Here’s a registry import file that you can use to do this. Copy the
following into a .reg file and run it
(Make sure that your .py extensions
are mapped to Python.File).
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Python.File\shellex\DropHandler]
#="{60254CA5-953B-11CF-8C96-00AA00B8708C}"
This makes Python scripts use the WSH drop handler, which is compatible with long filenames. To use the short filename handler, replace the GUID with 86C86720-42A0-1069-A2E8-08002B30309D.
A comment in that post indicates that one can enable dropping on "no console Python files (.pyw)" or "compiled Python files (.pyc)" by using the Python.NoConFile and Python.CompiledFile classes.

write a simple shell script (file.bat)
"C:\python27\python.exe" yourprogram.py %*
where %* stands for the all arguments you pass to the script.
Now drag & drop your target files on the file.bat icon.

With an installed python - at least 2.6.1 - you can just drag and drop any file on a python script.
import sys
droppedFile = sys.argv[1]
print droppedFile
sys.argv[0] is the script itself. sys.argv[n+1] are the files you have dropped.

Try using py2exe. Use py2exe to convert your python script into a windows executable. You should then be able to drag and drop input files to your script in Windows Explorer. You should also be able to create a shortcut on your desktop and drop input files onto it. And if your python script can take a file list you should be able to drag and drop multiple files on your script (or shortcut).

Create a shortcut of the file. In case you don't have python open .py files by default, go into the properties of the shortcut and edit the target of the shortcut to include the python version you're using. For example:
Target: C:\Python26\python.exe < shortcut target path>
I'm posting this because I didn't want to edit the Registry and the .bat workaround didn't work for me.

1). create shortcut of .py
2). right click -> properties
3). prefix "Target:" with "python" so it runs the .py as an argument into the python command
or
1). create a .bat
2). python some.py %*
these shortcut versions are simplest for me to do what i'm doing
otherwise i'd convert it to a .exe, but would rather just use java or c/c++

Late answer but none of the answers on this page worked for me.
I managed to enable/fix the drag and drop onto .py scripts using:
HKEY_CLASSES_ROOT\.py -> Set default value to Python.File
HKEY_CLASSES_ROOT\Python.File\Shell\Open -> Create a key called Command with default value "C:\Windows\py.exe" "%1" %*
CLASSES_ROOT\Applications\py.exe\open\command -> Create keys if the don't exist and set the default value to "C:\Windows\py.exe" "%1" %*
CLASSES_ROOT\Python.File\ShellEx -> create key DropHandler with default value {86C86720-42A0-1069-A2E8-08002B30309D}
That's it. Test it by dragging a file onto the python script:
import sys
args = sys.argv
print(args)

For those who use argv in .py script but still can't drag files to execute,
this could be solved by simply using Python Launcher (with rocket icon)
the script property "Open File" was set as python.exe,
which has no knowledge that the script needs command-line arguments "%*"
Refer to: https://bugs.python.org/issue40253

Related

Call a Python script (in a directory in the PATH) from command line with Windows

With Linux, if you create a file mycommand with +x permission in a folder which is in the PATH, containing a Python shebang #!..., then calling mycommand from command line works, no matter the current directory.
How to have the equivalent on Windows? i.e. I have a folder D:\myscripts\ which is in the PATH, containing a few personal scripts like mycommand.py.
How to make that calling mycommand or maybe just python mycommand from commandline will work, no matter the current working directory?
TL;DR: I'd like to avoid to have to do
python D:\myscripts\command.py
each time I want to call every-day-use personal scripts from D:\myscripts\
Note: .py files are associated with my text editor (i.e. when double-clicking on a .py file, it is opened with the text editor) and I want to keep this.
Solution 1
I finally did like this:
put D:\myscripts\ in the PATH (with setx, see How to update system PATH variable permanently from cmd?)
create a batch file: D:\myscripts\mycommand.bat containing:
#python d:\myscripts\mycommand.py
That's it!
Solution 2
Here is a solution from various comments from #ErykSun:
Open the system environment variables editor and ensure that "D:\myscripts" is in PATH (do not use quotes) and ".PY" is in PATHEXT (do not use quotes).
Create a test file D:\myscripts\test_command.py with the line import sys; print(sys.executable); print(sys.argv).
In a new command prompt that was opened from Explorer (to get the updated environment variables), run the test script from another directory as test_command spam eggs. If it runs with the expected Python installation and the command-line arguments "spam" and "eggs" are correctly passed, then you're done.
When double-clicking and running at the command prompt, the shell executes the default action for the filetype. If the filetype doesn't explicitly define a default action, the shell uses the "open" action, and if that's not defined it uses the filetype's first defined action, whatever that is. Python configures an "open" action for its "Python.File" filetype and additional context-menu (right-click) actions for editing with IDLE.
There are other directions you can go with this. You can use shell links. Add ".LNK" to PATHEXT and remove ".PY". Then for each script that you want to run, create a shell link (.LNK shortcut file) in D:\myscripts that runs the script explicitly as "path\to\python.exe" "\path\to\script". Leave the working directory field empty, so that it inherits the working directory of the parent process. For example, create D:\myscripts\test_command.lnk to run D:\myscripts\test_command.py.
In this case, you would run the script as test_command, which will find and execute "test_command.lnk". To get the default Sublime edit action, you would explicitly run test_command.py with the ".PY" extension.
Solution 3
Create an .exe file in c:\python37\scripts\ with this method: How to create a .exe similar to pip.exe, jupyter.exe, etc. from C:\Python37\Scripts?
but it's more complicated since it requires a package, etc.

How to run python script without writing command on cmd again and again

I am practicing python code and executing it through command line. each time i have to execute my python file i go to cmd and write
D:\PythonPractice>python myClass.py
Is there any standard and comfortable way available to execute python code quickly?
If you are executing the same command, with no changes to arguments or anything, you can pack it in a .bat file (windows executable)
Name it something like myscript.bat
D:\PythonPractice>python C:\path\to\myClass.py
Put full path inside it. Now double click will do.
Use PyCharm Software by JetBrains (same company who developed Android studio and Kotlin language), it will help you in many ways .
Run Python with single press of a button.
Add modules easily just with some clicks.
Debugging the code as smooth as possible.
It is Awesome, I am using it for past couple of months.
you can change file association which controls what to do when invoking filename in command line. For instance, when you just type text filename in cmd, notepadd will be opened, for word docs Word or other document editor.
Have a look into following link from google or you can fiddle by yourself starting from Control Panel > Control Panel Home > Default Programs > Set Associations. Select a file type in the list and click Change Program.
Alternatively,you can use any of Python IDE (PyCharm,PyDev etc.) so you will be able to run directly scripts from editor.
With Notepad++, you can also create a shortcut to be able to launch your python script :
Menu Run > Run (or press F5)
Choose the python .exe (default in C:\Program Files (x86)\Python36-32\python.exe
Then add this code next to the path : "$(FULL_CURRENT_PATH)"
This will allow to execute the current file
example of command with Python 3.6 :
"C:\Program Files (x86)\Python36-32\python.exe" "$(FULL_CURRENT_PATH)"
Then save the command, by assigning a shorcut key, it's better with a modifier like SHIFT (SHIFT + F10)
Now you can launch any script with this shortcut.

Running Python file by double-click

I have really annoying problem, I cannot run a Python file just by double-clicking.
I have tried to set it to open the file with idle.bat but that only starts IDLE editor on double-click, it does not run the Python file.
What version of Python do you have installed?
You should write your own batch file to execute your python binary and your script.
For example, with a default Python 2.7 installation on Windows, this could be the entire contents of your script.
myscript.bat:
ECHO ON
REM A batch script to execute a Python script
SET PATH=%PATH%;C:\Python27
python yourscript.py
PAUSE
Save this file as "myscript.bat" (make sure it's not "myscript.bat.txt"), then double click it.
right click on the file->open with->choose default program->more options->select python.exe file and click on.
Right click the file, select open with. If you want to simply run the script, find python.exe and select it. If you want to debug with IDLE, find that executable and select it.
When I had both Py2 and Py3, and then removed the former, my script wouldn't run by double-clicking it either (but fine from console.) I realized my __pycache__ folder (same directory as the script) was the issue. Problem solved when deleted.
You can also start a Django app this way. Once the Django server starts it enters a "wait" kind of mode so a batch file only requires two lines:
ECHO ON
python manage.py runserver
Manage.py can be in any directory, just keep the full folder path in the command within the batch file:
ECHO ON
python C:\temp\manage.py runserver
Solution for Ubuntu users.
Right-click on the file, then select open with, then choose your python version, it shode be in
/bin folder, usually it's /bin/python3.exe
In Windows 10, using regedit:
Under HKEY_CLASSES_ROOT, create key: .py
Edit Default string value to be py_auto_file (or any other name you want to call it)
Under HKCR, create another key: py_auto_file (or any matching name that you have just picked)
Under this key, create nested sub-keys: shell --> open --> command.
Edit Default string value to "C:\path\to\your\python.exe" "%1"

Notepad++ cannot find filepath

I'm trying to run a Hello World program in Python I wrote in Notepad++ using the NppExec plugin, but instead of printing, I'm getting
python C:\Users\Sam\Desktop\Test.py
CreateProcess() failed with error code 2:
The system cannot find the file specified.
The argument I'm giving NppExec is
python C:\Users\Sam\Desktop\Test.py
which is the filepath that NP++ gives me when I copy the full filepath to the clipboard.
Is there some configuration of NP++ that I have to set to get this to work?
I tried what you are attempting to do, and this is how I solved it:
Instead of passing the argument you gave, I passed this one:
C:\Python32\python.exe C:\Users\Sam\Desktop\Test.py for python 3+
C:\Python27\python.exe C:\Users\Sam\Desktop\Test.py for python 2
Generally, in order for it to work, you have to define where you have installed the python executable.
In general, you can use the following as an argument to NppExec for any currently opened Python script in Notepad++:
[Your Python install folder here]\python.exe "$(FULL_CURRENT_PATH)"
Note that "FULL_CURRENT_PATH" is a Notepad++ internal variable, not a placeholder for your file's actual path and filename, so the above argument should work without edits regardless of your current script's filename.
Further references for using NppExec with other source code: http://sourceforge.net/apps/mediawiki/notepad-plus/index.php?title=Compiling_Source_Code
This is an alternative method to running python programs in notepad++, which I recommend after being unable to find a suitable plugin.
create a batch file called pythonXX.bat ( where XX is the current version of python you're using ) and save it along side your python.exe in C:\PythonXX\
and insert this text into that batch file:
#ECHO OFF
C:\PythonXX\python.exe "%1"
PAUSE
#ECHO ON
Then inside notepad++ create a run command:
C:\PythonXX\pythonXX.bat "$(FULL_CURRENT_PATH)"
Then click save run and assign it to a keyboard short-cut, good to go :)

how to configure the hotkey of notepad++ for python?

hi I am using notepad++ for python.
I want a hotkey that will run my python script at the path of the script instead of the path of notepad++.
Besides, at the end of process, i want it to show "press any key to continue".
I've tried:
cmd /k "$(FULL_CURRENT_PATH)"
python "$(FULL_CURRENT_PATH)"
But none of them meets my expectations.
For example:
f = open('hello.txt', 'w')
f.close()
This will create 'hello.txt' in the directory of notepad++, but I want to create it at the same path of this script.
That's a good find - Notepad++ seems to always set the current working directory to it's own executable directory when scripts are run.
One workaround is to use this: Notepad++ Python Plug-in. This is a plug-in for Notepad++ and adds options to run your scripts in Python from the Plugins menu in Notepad++ (instead of using the Run menu). This should fix your current working directory problem.

Categories

Resources