Write how to create a wrapper file in a Windows environment. Normally, this should be done when executing from a command prompt or the like. (Assuming that the PATH of the directory where python.exe is located is in the PATH)
C:\Users\testuser> python C:\Users\testUser\pyruntest.py
→pyruntest.py execution result is output
But if you don't have python.exe associated with your .py file, you can't do this.
C:\Users\testuser> pyruntest.py
→ Error
@echo off
%PYTHONPATH%\python.exe %~dp0\%~n0.py %*
For example, if you save the above with the file name pyruntest.bat You can run pyruntest.py in the same directory as pyruntest.bat as follows:
C:> pyruntest.bat
→pyruntest.py execution result is output
You can go from anywhere if you go through Path If you don't want to type .bat, you can change the extension to .cmd.
C:> pyruntest
→pyruntest.py execution result is output
There is usually no problem with method 1, but in order to process multiple files you want to process as arguments When I drag and drop it to .cmd, I get this error.
You specified too many command line arguments for the command. Specify the correct value and try again.
The cause is the limitation of cmd.exe? 2047 characters or 8191 characters (WinXP or later) seems to be the maximum. Powershell doesn't have this limit, but it's a thorny way to run .ps1 with a double click. .. I wonder what a poor terminal it is, and I wish Powershell, a command prompt, would disappear from the world. It should be possible to solve it if it is converted to an exe.
Probably the only way to use it in such a case is to use the exe executable format. There are various ways to make it into an exe, but
After all, I thought I would make it with Javascript, but I was frustrated and decided to make it with C #.
pyruntest.cs I'm a C # beginner and there are many things I don't understand, but I've moved so I'll expose it.
using System;
using System.Diagnostics;
using System.ComponentModel;
class Program
{
static void Main(string[] args)
{
// Python Program Path
var pythonScriptPath = System.Reflection.Assembly.GetExecutingAssembly().Location.Replace(".exe", "") + ".py";
try
{
var startInfo = new ProcessStartInfo()
{
FileName = @"python.exe",
UseShellExecute = false,
Arguments = "\"" + pythonScriptPath + "\" \"" + string.Join("\" \"", args),
};
using (var process = Process.Start(startInfo))
{
process.WaitForExit();
}
}
catch (Win32Exception)
{
Console.WriteLine("python.The exe was not found. Add to environment variable PATH");
}
}
}
Compiling with csc.exe produces pyruntest.exe
C:\Users\testuser> C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe pyruntest.cs
C:\Users\testuser> pyruntest.exe
→pyruntest.py execution result is output
C:\Users\testuser> pyruntest
→ Of course this is still pyruntest.py execution result is output
If you put it through the environment variable path properly, it can be executed regardless of the current directory
C:\Users\testuser> set PATH=%PATH%;%CD%
C:\Users\testuser> setx PATH %PATH%
C:\Users\testuser> cd \
C:> pyruntest
→pyruntest.py execution result is output
Method 1 and method 2 have a common mechanism, Common specifications for the above execution method: Place [I want to execute] .bat or [I want to execute] .cmd or [I want to execute] .exe in the same directory as [I want to execute] .py. (Now you don't have to modify the wrapper source every time)
Aside) The reason why I'm doing this is that if it's a home PC, it's over if you set the association. In a thin client environment for work, I could not change the settings without authority, so You need such a roundabout wrapper. It has become an inconvenient world.
Recommended Posts