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










share|improve this question
























  • 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

















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










share|improve this question
























  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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














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.






share|improve this answer





















  • 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


















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.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer





















    • 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















    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.






    share|improve this answer





















    • 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













    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.






    share|improve this answer












    Have you tried changing the account the website uses in IIS? As it could be a permission issue if its using the networkservice account.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • 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












    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.






    share|improve this answer

























      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.






      share|improve this answer























        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 11 at 2:38









        Marco Antonio Lea Plaza Soruco

        14




        14






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ