1

I have a bunch a python scripts inside a folder with other files. I'm running them from batch file in order. Problem is that those are all .py files that you can run just by double click and if I accidentally run any of them it will mess everything up. Is there any way to disable double click for .py files but still be able to run them from cmd/batch.

I have python 3.9.5 and Windows 10 1909.

5
  • Did you try what superuser.com/a/49617/155147 suggests? Commented Jul 28, 2021 at 11:00
  • Why not make your code more robust so as to not “mess things up” when it’s started in the wrong way, instead of breaking your own system by removing file associations (that could re-appear during an update and can be circumvented)? You will thank yourself later… You could set an environment variable in the batch file and see if it exists in your Python script for example, or you could check for command line parameters to be present and valid.
    – StarCat
    Commented Jul 29, 2021 at 6:27
  • assoc .py=txtfile - after that, doubleclicking opens them in notepad (if you didn't change ftype txtfile). Note the output of assoc .py before, in case you want to revert it sometimes in the future.
    – Stephan
    Commented Jul 29, 2021 at 7:27
  • @StarCat "You could set an environment variable in the batch file and see if it exists in your Python script for example". That would be nice and example code for that will be nicer xD
    – IGRACH
    Commented Jul 29, 2021 at 9:10
  • If you don't want to do it globally (for each and every .py file), you can call your phyton script with a (dummy) parameter from the batchfile. By executing with a double-click, there are no parameters. So just check for parameters in your phyton script and exit, when there are none.
    – Stephan
    Commented Jul 29, 2021 at 13:57

1 Answer 1

0

Is there any way to disable double click for .py files but still be able to run them from cmd/batch?

I believe the only solution for disabling double-click execution is to disassociate the .py file extension from Python entirely. How you might go about that could be different depending on what Python distribution you have installed (for instance, with CPython from python.org, py.exe is typically what manages Python script execution, rather than a direct Windows association between a given python.exe and .py files).

Regarding cmd\batch execution, that would simply be e.g.:

C:\path\to\your\python.exe script.py option1 option2 ...

That is, double-click and cmd\batch execution are completely separate, just as with non-py files.

If you have a preferred version of Python available at the command line (e.g. as python), then you could likely use that in place of the full path to python.exe.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .