Basj
Basj

Reputation: 46463

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?

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.

Upvotes: 3

Views: 7071

Answers (1)

Basj
Basj

Reputation: 46463

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.

Upvotes: 1

Related Questions