Running python script in through a bat file in a ASP.NET Web API
up vote
0
down vote
favorite
I'm trying to run a python script. It runs a trained model from keras.
I worked with a C# Web API. I've cracked my head trying to make the python script run. I've made it through a .bat file. It works perfectly on a windows form project. And the bat itself works fine as well.
The issue comes when the Web API wants to run the .bat file, for some reason
it when I run the script through IIS Express it won't recognize the data.csv file that the model is supposed to read.
Let me show you some code
Run the bat file
System.Diagnostics.Process.Start(@"pathtobatfilerunner.bat");
bat file
pathtoenviromentpython.exe pathtoscriptAI.py %*
PAUSE
Allright, so when I run it outside IIS Express it works fine but when I run it on the Web API ...
Traceback (most recent call last):
File "D:AIASSETSAI.py", line 13, in
q = pd.read_csv('data.csv')
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 787, in init
self._make_engine(self.engine)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1708, in init
self._reader = parsers.TextReader(src, **kwds)
File "pandas_libsparsers.pyx", line 384, in pandas._libs.parsers.TextReader.cinit
File "pandas_libsparsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'data.csv' does not exist
but it does actually exist, this just happens when I run it from the Web API.
some extra information: the bat file takes a long time to run the python script. but the script itself runs instantaneously.
the data.csv file is generated through code and it generates correctly. Could it be that the file takes too long to write itself and the batch file runs before the data.csv is written? I made some tests in another project but the data.csv was written instantaneously.
Thanks in advance
Edit:
I've just checked debugging and even with a Thread sleep that the data.csv is not taking too long to build. IIS console just won't let the python script read the data.csv file
c# python batch-file
add a comment |
up vote
0
down vote
favorite
I'm trying to run a python script. It runs a trained model from keras.
I worked with a C# Web API. I've cracked my head trying to make the python script run. I've made it through a .bat file. It works perfectly on a windows form project. And the bat itself works fine as well.
The issue comes when the Web API wants to run the .bat file, for some reason
it when I run the script through IIS Express it won't recognize the data.csv file that the model is supposed to read.
Let me show you some code
Run the bat file
System.Diagnostics.Process.Start(@"pathtobatfilerunner.bat");
bat file
pathtoenviromentpython.exe pathtoscriptAI.py %*
PAUSE
Allright, so when I run it outside IIS Express it works fine but when I run it on the Web API ...
Traceback (most recent call last):
File "D:AIASSETSAI.py", line 13, in
q = pd.read_csv('data.csv')
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 787, in init
self._make_engine(self.engine)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1708, in init
self._reader = parsers.TextReader(src, **kwds)
File "pandas_libsparsers.pyx", line 384, in pandas._libs.parsers.TextReader.cinit
File "pandas_libsparsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'data.csv' does not exist
but it does actually exist, this just happens when I run it from the Web API.
some extra information: the bat file takes a long time to run the python script. but the script itself runs instantaneously.
the data.csv file is generated through code and it generates correctly. Could it be that the file takes too long to write itself and the batch file runs before the data.csv is written? I made some tests in another project but the data.csv was written instantaneously.
Thanks in advance
Edit:
I've just checked debugging and even with a Thread sleep that the data.csv is not taking too long to build. IIS console just won't let the python script read the data.csv file
c# python batch-file
Isn't it easier to add the IronPython NuGet package to your project and run your script directly?
– Восилей
Nov 10 at 23:39
I tried IronPython, but it's super complicated to install libraries to IronPython, and as far as I know you can't install other libraries that are numpy and scipy. and I need keras and pandas. So for now IronPython is not the way to go for me...
– Marco Antonio Lea Plaza Soruco
Nov 10 at 23:50
I had a little experience, when there was no other way to interact with external application except running the other process but I didn't save any files on disk, I just passed all the information through the command-line arguments, then read all of the information from stdout. Maybe it will worth trying.
– Восилей
Nov 11 at 0:02
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to run a python script. It runs a trained model from keras.
I worked with a C# Web API. I've cracked my head trying to make the python script run. I've made it through a .bat file. It works perfectly on a windows form project. And the bat itself works fine as well.
The issue comes when the Web API wants to run the .bat file, for some reason
it when I run the script through IIS Express it won't recognize the data.csv file that the model is supposed to read.
Let me show you some code
Run the bat file
System.Diagnostics.Process.Start(@"pathtobatfilerunner.bat");
bat file
pathtoenviromentpython.exe pathtoscriptAI.py %*
PAUSE
Allright, so when I run it outside IIS Express it works fine but when I run it on the Web API ...
Traceback (most recent call last):
File "D:AIASSETSAI.py", line 13, in
q = pd.read_csv('data.csv')
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 787, in init
self._make_engine(self.engine)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1708, in init
self._reader = parsers.TextReader(src, **kwds)
File "pandas_libsparsers.pyx", line 384, in pandas._libs.parsers.TextReader.cinit
File "pandas_libsparsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'data.csv' does not exist
but it does actually exist, this just happens when I run it from the Web API.
some extra information: the bat file takes a long time to run the python script. but the script itself runs instantaneously.
the data.csv file is generated through code and it generates correctly. Could it be that the file takes too long to write itself and the batch file runs before the data.csv is written? I made some tests in another project but the data.csv was written instantaneously.
Thanks in advance
Edit:
I've just checked debugging and even with a Thread sleep that the data.csv is not taking too long to build. IIS console just won't let the python script read the data.csv file
c# python batch-file
I'm trying to run a python script. It runs a trained model from keras.
I worked with a C# Web API. I've cracked my head trying to make the python script run. I've made it through a .bat file. It works perfectly on a windows form project. And the bat itself works fine as well.
The issue comes when the Web API wants to run the .bat file, for some reason
it when I run the script through IIS Express it won't recognize the data.csv file that the model is supposed to read.
Let me show you some code
Run the bat file
System.Diagnostics.Process.Start(@"pathtobatfilerunner.bat");
bat file
pathtoenviromentpython.exe pathtoscriptAI.py %*
PAUSE
Allright, so when I run it outside IIS Express it works fine but when I run it on the Web API ...
Traceback (most recent call last):
File "D:AIASSETSAI.py", line 13, in
q = pd.read_csv('data.csv')
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 678, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 440, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 787, in init
self._make_engine(self.engine)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1014, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:UsersMarcoAnaconda3envsentornotensorlibsite-packagespandasioparsers.py", line 1708, in init
self._reader = parsers.TextReader(src, **kwds)
File "pandas_libsparsers.pyx", line 384, in pandas._libs.parsers.TextReader.cinit
File "pandas_libsparsers.pyx", line 695, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: File b'data.csv' does not exist
but it does actually exist, this just happens when I run it from the Web API.
some extra information: the bat file takes a long time to run the python script. but the script itself runs instantaneously.
the data.csv file is generated through code and it generates correctly. Could it be that the file takes too long to write itself and the batch file runs before the data.csv is written? I made some tests in another project but the data.csv was written instantaneously.
Thanks in advance
Edit:
I've just checked debugging and even with a Thread sleep that the data.csv is not taking too long to build. IIS console just won't let the python script read the data.csv file
c# python batch-file
c# python batch-file
edited Nov 10 at 23:52
asked Nov 10 at 23:34
Marco Antonio Lea Plaza Soruco
14
14
Isn't it easier to add the IronPython NuGet package to your project and run your script directly?
– Восилей
Nov 10 at 23:39
I tried IronPython, but it's super complicated to install libraries to IronPython, and as far as I know you can't install other libraries that are numpy and scipy. and I need keras and pandas. So for now IronPython is not the way to go for me...
– Marco Antonio Lea Plaza Soruco
Nov 10 at 23:50
I had a little experience, when there was no other way to interact with external application except running the other process but I didn't save any files on disk, I just passed all the information through the command-line arguments, then read all of the information from stdout. Maybe it will worth trying.
– Восилей
Nov 11 at 0:02
add a comment |
Isn't it easier to add the IronPython NuGet package to your project and run your script directly?
– Восилей
Nov 10 at 23:39
I tried IronPython, but it's super complicated to install libraries to IronPython, and as far as I know you can't install other libraries that are numpy and scipy. and I need keras and pandas. So for now IronPython is not the way to go for me...
– Marco Antonio Lea Plaza Soruco
Nov 10 at 23:50
I had a little experience, when there was no other way to interact with external application except running the other process but I didn't save any files on disk, I just passed all the information through the command-line arguments, then read all of the information from stdout. Maybe it will worth trying.
– Восилей
Nov 11 at 0:02
Isn't it easier to add the IronPython NuGet package to your project and run your script directly?
– Восилей
Nov 10 at 23:39
Isn't it easier to add the IronPython NuGet package to your project and run your script directly?
– Восилей
Nov 10 at 23:39
I tried IronPython, but it's super complicated to install libraries to IronPython, and as far as I know you can't install other libraries that are numpy and scipy. and I need keras and pandas. So for now IronPython is not the way to go for me...
– Marco Antonio Lea Plaza Soruco
Nov 10 at 23:50
I tried IronPython, but it's super complicated to install libraries to IronPython, and as far as I know you can't install other libraries that are numpy and scipy. and I need keras and pandas. So for now IronPython is not the way to go for me...
– Marco Antonio Lea Plaza Soruco
Nov 10 at 23:50
I had a little experience, when there was no other way to interact with external application except running the other process but I didn't save any files on disk, I just passed all the information through the command-line arguments, then read all of the information from stdout. Maybe it will worth trying.
– Восилей
Nov 11 at 0:02
I had a little experience, when there was no other way to interact with external application except running the other process but I didn't save any files on disk, I just passed all the information through the command-line arguments, then read all of the information from stdout. Maybe it will worth trying.
– Восилей
Nov 11 at 0:02
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
Have you tried changing the account the website uses in IIS? As it could be a permission issue if its using the networkservice account.
could you elaborate on how to change the account please? I can't find good information so far
– Marco Antonio Lea Plaza Soruco
Nov 11 at 1:44
open IIS, select the site in question, go to basic settings then click 'connect as'
– Bobby Bridgeman
Nov 11 at 2:23
add a comment |
up vote
0
down vote
I found out what the issue was.
as I'm running the script outside the environment, and I'm calling the interpreter from a Process class, there's no way for the script to know what is the working folder. It assumes it to be where the interpreter is. the solution is very straight forward, just add the path in the script.
something like this:
q = pd.read_csv('D:\AIASSETS\data.csv')
and wherever you use a file add "D:AIASSETS" or better said whatever your working folder is.
even in your model or if you're writing a file.
I'm not to worded on python, but I believe there's a way to set the working folder by code as well, that might work too.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Have you tried changing the account the website uses in IIS? As it could be a permission issue if its using the networkservice account.
could you elaborate on how to change the account please? I can't find good information so far
– Marco Antonio Lea Plaza Soruco
Nov 11 at 1:44
open IIS, select the site in question, go to basic settings then click 'connect as'
– Bobby Bridgeman
Nov 11 at 2:23
add a comment |
up vote
0
down vote
Have you tried changing the account the website uses in IIS? As it could be a permission issue if its using the networkservice account.
could you elaborate on how to change the account please? I can't find good information so far
– Marco Antonio Lea Plaza Soruco
Nov 11 at 1:44
open IIS, select the site in question, go to basic settings then click 'connect as'
– Bobby Bridgeman
Nov 11 at 2:23
add a comment |
up vote
0
down vote
up vote
0
down vote
Have you tried changing the account the website uses in IIS? As it could be a permission issue if its using the networkservice account.
Have you tried changing the account the website uses in IIS? As it could be a permission issue if its using the networkservice account.
answered Nov 10 at 23:59
Bobby Bridgeman
312
312
could you elaborate on how to change the account please? I can't find good information so far
– Marco Antonio Lea Plaza Soruco
Nov 11 at 1:44
open IIS, select the site in question, go to basic settings then click 'connect as'
– Bobby Bridgeman
Nov 11 at 2:23
add a comment |
could you elaborate on how to change the account please? I can't find good information so far
– Marco Antonio Lea Plaza Soruco
Nov 11 at 1:44
open IIS, select the site in question, go to basic settings then click 'connect as'
– Bobby Bridgeman
Nov 11 at 2:23
could you elaborate on how to change the account please? I can't find good information so far
– Marco Antonio Lea Plaza Soruco
Nov 11 at 1:44
could you elaborate on how to change the account please? I can't find good information so far
– Marco Antonio Lea Plaza Soruco
Nov 11 at 1:44
open IIS, select the site in question, go to basic settings then click 'connect as'
– Bobby Bridgeman
Nov 11 at 2:23
open IIS, select the site in question, go to basic settings then click 'connect as'
– Bobby Bridgeman
Nov 11 at 2:23
add a comment |
up vote
0
down vote
I found out what the issue was.
as I'm running the script outside the environment, and I'm calling the interpreter from a Process class, there's no way for the script to know what is the working folder. It assumes it to be where the interpreter is. the solution is very straight forward, just add the path in the script.
something like this:
q = pd.read_csv('D:\AIASSETS\data.csv')
and wherever you use a file add "D:AIASSETS" or better said whatever your working folder is.
even in your model or if you're writing a file.
I'm not to worded on python, but I believe there's a way to set the working folder by code as well, that might work too.
add a comment |
up vote
0
down vote
I found out what the issue was.
as I'm running the script outside the environment, and I'm calling the interpreter from a Process class, there's no way for the script to know what is the working folder. It assumes it to be where the interpreter is. the solution is very straight forward, just add the path in the script.
something like this:
q = pd.read_csv('D:\AIASSETS\data.csv')
and wherever you use a file add "D:AIASSETS" or better said whatever your working folder is.
even in your model or if you're writing a file.
I'm not to worded on python, but I believe there's a way to set the working folder by code as well, that might work too.
add a comment |
up vote
0
down vote
up vote
0
down vote
I found out what the issue was.
as I'm running the script outside the environment, and I'm calling the interpreter from a Process class, there's no way for the script to know what is the working folder. It assumes it to be where the interpreter is. the solution is very straight forward, just add the path in the script.
something like this:
q = pd.read_csv('D:\AIASSETS\data.csv')
and wherever you use a file add "D:AIASSETS" or better said whatever your working folder is.
even in your model or if you're writing a file.
I'm not to worded on python, but I believe there's a way to set the working folder by code as well, that might work too.
I found out what the issue was.
as I'm running the script outside the environment, and I'm calling the interpreter from a Process class, there's no way for the script to know what is the working folder. It assumes it to be where the interpreter is. the solution is very straight forward, just add the path in the script.
something like this:
q = pd.read_csv('D:\AIASSETS\data.csv')
and wherever you use a file add "D:AIASSETS" or better said whatever your working folder is.
even in your model or if you're writing a file.
I'm not to worded on python, but I believe there's a way to set the working folder by code as well, that might work too.
answered Nov 11 at 2:38
Marco Antonio Lea Plaza Soruco
14
14
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244474%2frunning-python-script-in-through-a-bat-file-in-a-asp-net-web-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Isn't it easier to add the IronPython NuGet package to your project and run your script directly?
– Восилей
Nov 10 at 23:39
I tried IronPython, but it's super complicated to install libraries to IronPython, and as far as I know you can't install other libraries that are numpy and scipy. and I need keras and pandas. So for now IronPython is not the way to go for me...
– Marco Antonio Lea Plaza Soruco
Nov 10 at 23:50
I had a little experience, when there was no other way to interact with external application except running the other process but I didn't save any files on disk, I just passed all the information through the command-line arguments, then read all of the information from stdout. Maybe it will worth trying.
– Восилей
Nov 11 at 0:02