0

I am trying to call a python script from my asp.net web app. I am using a batch file to call the python script. The python script runs from the batch file when I run it on the server. When I try to call the batch file from asp.net, the code in the batch file works until it gets to the call for the python, and the call to the python script doesn't work.

This is my batch file:

(
  echo Hello
)
C:\inetpub\Sites\**\**\**\test.txt
rem the code above works from ASP.NET

rem this code is never called
set CONDAPATH="C:\\Users\\***\\Anaconda3"
set ENVNAME="base"
if %ENVNAME%==base (set ENVPATH=%CONDAPATH%) else (set ENVPATH=%CONDAPATH%\envs\%ENVNAME%)
call %CONDAPATH%\Scripts\activate.bat %ENVPATH%
C:\\Users\\***\\Anaconda3\\python.exe C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py

Could anyone help me figure out what I am doing wrong in calling this python code?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • 1
    `ENVNAME` is not equal to `base`, it is equal to `"base"`. You should learn to use the recommended syntax for defining variables, and to use doublequotes around your file paths. The recommended syntax is `set "varname=valstring"`. – Compo Jan 30 '21 at 15:21

2 Answers2

1

The first thing you need to understand is that your line:

C:\inetpub\Sites\**\**\**\test.txt

will open a text file in the default application for doing so, and your script will then wait until you have closed that text file, before it continues to the next line. If you want the code to continue without waiting for the text file to be closed, you can use the start command:

Start "" "C:\inetpub\Sites\**\**\**\test.txt"

Here's a quick example to demonstrate my earlier comment, regarding the recommended syntax for defining variables, and using doublequotes around file paths.

Set "CONDAPATH=%UserProfile%\Anaconda3"
Set "ENVNAME=base"
If /I "%ENVNAME%" == "base" (Set "ENVPATH=%CONDAPATH%") Else Set "ENVPATH=%CONDAPATH%\envs\%ENVNAME%"
Call "%CONDAPATH%\Scripts\activate.bat" "%ENVPATH%"
"%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"

Putting it all together:

@Echo Off
Echo Hello
Start "" "C:\inetpub\Sites\**\**\**\test.txt"
Set "CONDAPATH=%UserProfile%\Anaconda3"
Set "ENVNAME=base"
If /I "%ENVNAME%" == "base" (Set "ENVPATH=%CONDAPATH%") Else Set "ENVPATH=%CONDAPATH%\envs\%ENVNAME%"
Call "%CONDAPATH%\Scripts\activate.bat" "%ENVPATH%"
"%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"

If you don't want the script to wait until the python command has completed, (for instance, if this is the last line of your script), you could change that last line to:

Start "" "%CONDAPATH%\python.exe" "C:\\inetpub\\Sites\\**\\***\\testfolder\\try_py.py"
Compo
  • 36,585
  • 5
  • 27
  • 39
  • Thank you so much for the help in cleaning up that syntax. I made those changes. It works when I call that batch file from the cmd line on that server. When I try to call it in my asp.net code though, the hello is written to the test.txt so I know that the batch script is being called by asp.net. The asp.net is not liking the call to execute the python script. Any thoughts on that? – Suzi King Jan 30 '21 at 18:20
  • Well @SuziKing, perhaps the paths etc. you are using are the issue. As I cannot know that, I'm unable to assist you with a fix for it. For all I know the end users `\Scripts\activate.bat` or `python.exe` directory is really `%LocalAppData%\Continuum\Anaconda3`, or `%ProgramData%\Anaconda3`. Those are not an issue with my script above, _(posted to show you the correct syntax)_, they're related only to the information you provided. I also have no idea whether you can run those executables from ASP.NET, because I don't use it, and would never ruin a Windows installation by putting python on it! – Compo Jan 30 '21 at 18:58
0

I figured it out in case anyone is still using asp.net like me. It was all a permissions issue on my folder that had I was calling the python script from CONDAPATH="C:\Users\***\Anaconda3".

This link gave me the hint: IIS7 Permissions Overview - ApplicationPoolIdentity