3

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.

8
  • Do you mean something like an "alisas" to just call your command and take you to some directory? Commented May 23, 2020 at 8: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. Commented May 23, 2020 at 8: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. Commented May 23, 2020 at 9: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. Commented May 23, 2020 at 9: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". Commented May 23, 2020 at 10:00

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.