How do you run a Python script that is called from a .bat file as a Visual Studio pre-build event?

Ollie 41 Reputation points
2022-11-20T17:26:05.997+00:00

I am calling a .bat file as a Visual Studio pre-build event. In Visual Studio, project > properties > build events > pre-build events, I have the following call:

cmd /c ""C:\Program Files (x86)\MyBatchFile.bat""  

The .bat script runs fine until it reaches a line that calls a Python script (from inside the .bat file). Then the console repeatedly outputs an error: 'Could not decode the provided text'.

The line in the .bat file that triggers the error is shown here (bottom line):

echo Setting Plugin Type...  
set PluginType=--source  
echo Setting Project Name...  
set ProjectName=-n Name  
echo Setting display name...  
set DisplayName=  
echo Setting author...  
set Author=-a Author  
echo Setting description...  
set Description=-d "Description"  
echo Setting 'no-prompt' for confirming new plugin setup...  
set Confirm=--no-prompt   
python "C:\Program Files (x86)\wp.py" new %PluginType% %ProjectName% %DisplayName% %Author% %Description% %Confirm%  

I have tried modifying the .bat file so that the word 'python' is on one line, and the next line calls the python script, like this:

python  
"C:\Program Files (x86)\wp.py" new %PluginType% %ProjectName% %DisplayName% %Author% %Description% %Confirm%  

This prevents the error from outputting, but the console instead says 'python not found'. I have Python in my environment variables, and it works fine when I open a command line and type 'python'. The .bat file also works fine when I double click it in file explorer to run it.

I can't figure out why when Visual Studio calls my .bat file it cannot detect python installed on my machine.

Any ideas how to fix this?

Community Center Not monitored
0 comments No comments
{count} votes

Accepted answer
  1. Tianyu Sun-MSFT 34,436 Reputation points Microsoft External Staff
    2022-11-21T07:50:44.093+00:00

    Hi @Ollie ,

    Welcome to Microsoft Q&A forum.

    I did a quick test on my side and it seems that VS identified and found the python, and it worked well. If you try to add the specific path of python into the .bat file, will it work? For example: C:\XXXX\python.exe "C:\Program Files (x86)\wp.py" new xxxxxxxx.

    262463-image.png

    262496-image.png

    Feel free to contact us.

    Best Regards,
    Tianyu

    • If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
      Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.