Various ways to execute .py files on Windows

How to run a normal .py file

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

1. Make a wrapper with bat file or cmd file

@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

2. Make an exe in C #.

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

Various ways to execute .py files on Windows
Python 3.6 on Windows ... and to Xamarin.
How to use Dataiku on Windows
How to install pycrypto on Windows
How to deploy django-compressor on Windows
Bring files in Windows to WSL
How to install music 21 on windows
[Kivy] How to install Kivy on Windows [Python]
How to use Google Assistant on Windows 10
How to install richzhang / colorization on Windows 10
Various ways to create a dictionary (memories)
Verification of how to periodically execute a script on a Linux server on Windows
How to find large files on Linux
Various ways to destroy resources with scope
[Python] List Comprehension Various ways to create a list
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
Put MicroPython on Windows to run ESP32 on Python
How to quickly install h5py on Windows 10 [Unofficial]
Organize files on Windows with Linux commands-using WSL-
How to live a decent life on 2017 Windows
I want to do pyenv + pipenv on Windows
[Python] How to install OpenCV on Anaconda [Windows]
[Windows] Memo to use Keras on GPU [Tensorflow-GPU]
How to install / verify graphviz on anaconda / windows10
Steps to build PyTorch 1.5 for CUDA 10.2 on Windows
Python on Windows
Convert jupyter notebook .ipynb files to python executable .py files
Various ways to extract columns in a NumPy array
How to embed mod_wsgi into Apache on Python Windows
Try to solve Sudoku in various ways (SAT, CSP)
How to run Django on IIS on a Windows server
2 ways to read all csv files in a folder
How to operate Firefox with selenium on Windows Memo