0

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?

enter image description here

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.

Basj
  • 29,668
  • 65
  • 241
  • 451
  • Do you mean something like an "alisas" to just call your command and take you to some directory? – Pixele9 May 23 '20 at 08:44
  • 1
    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. – Eryk Sun May 23 '20 at 08:47
  • @ErykSun Also, to add some more difficulty, `.py` files are associated with SublimeText, i.e. double-clicking on them opens them with the text editor (and I want to keep this). (See edited question). Summary: 1) `test_command` works (but opens it in the text editor, and I would prefer to *run* it) 2) `python test_command`: file not found. – Basj May 23 '20 at 09:43
  • For simply running the file, you have to choose one or the other. For 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. – Eryk Sun May 23 '20 at 09:52
  • 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". – Eryk Sun May 23 '20 at 10:00
  • 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. – Eryk Sun May 23 '20 at 10:04
  • Great ideas @ErykSun. Do you want to post these comments together as an answer or do you prefer I do it (I will cite you if so)? – Basj May 23 '20 at 10:10
  • Feel free to add an answer. I know this question has been asked before, however. – Eryk Sun May 23 '20 at 10:28

1 Answers1

1

Solution 1

I finally did like this:

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.

Community
  • 1
  • 1
Basj
  • 29,668
  • 65
  • 241
  • 451