What is the recommended practice in django to execute external scripts?












1















I'm planning to build a WebApp that will need to execute scripts based on the argument that an user will provide in a text-field or in the Url.



possible solutions that I have found:




  1. create a lib directory in the root directory of the project, and put the scripts there, and import it from views.



  2. using subprocess module to directly run the scripts in the following way:



    subprocess.call(['python', 'somescript.py', argument_1,...])



argument_1: should be what an end user provides.










share|improve this question

























  • The second approach leaves you at the mercy of whatever the default python interpreter on the executing machine is. Say you wrote code for python3 and the server has python2.7 set as default, your scripts will all fail in very interesting ways.

    – BoboDarph
    Jan 3 at 14:44











  • removed irrelevant "django" and "drf" tags.

    – bruno desthuilliers
    Jan 3 at 14:55
















1















I'm planning to build a WebApp that will need to execute scripts based on the argument that an user will provide in a text-field or in the Url.



possible solutions that I have found:




  1. create a lib directory in the root directory of the project, and put the scripts there, and import it from views.



  2. using subprocess module to directly run the scripts in the following way:



    subprocess.call(['python', 'somescript.py', argument_1,...])



argument_1: should be what an end user provides.










share|improve this question

























  • The second approach leaves you at the mercy of whatever the default python interpreter on the executing machine is. Say you wrote code for python3 and the server has python2.7 set as default, your scripts will all fail in very interesting ways.

    – BoboDarph
    Jan 3 at 14:44











  • removed irrelevant "django" and "drf" tags.

    – bruno desthuilliers
    Jan 3 at 14:55














1












1








1








I'm planning to build a WebApp that will need to execute scripts based on the argument that an user will provide in a text-field or in the Url.



possible solutions that I have found:




  1. create a lib directory in the root directory of the project, and put the scripts there, and import it from views.



  2. using subprocess module to directly run the scripts in the following way:



    subprocess.call(['python', 'somescript.py', argument_1,...])



argument_1: should be what an end user provides.










share|improve this question
















I'm planning to build a WebApp that will need to execute scripts based on the argument that an user will provide in a text-field or in the Url.



possible solutions that I have found:




  1. create a lib directory in the root directory of the project, and put the scripts there, and import it from views.



  2. using subprocess module to directly run the scripts in the following way:



    subprocess.call(['python', 'somescript.py', argument_1,...])



argument_1: should be what an end user provides.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 3 at 14:55









bruno desthuilliers

51.7k54465




51.7k54465










asked Jan 3 at 14:39









ArximedeArximede

418




418













  • The second approach leaves you at the mercy of whatever the default python interpreter on the executing machine is. Say you wrote code for python3 and the server has python2.7 set as default, your scripts will all fail in very interesting ways.

    – BoboDarph
    Jan 3 at 14:44











  • removed irrelevant "django" and "drf" tags.

    – bruno desthuilliers
    Jan 3 at 14:55



















  • The second approach leaves you at the mercy of whatever the default python interpreter on the executing machine is. Say you wrote code for python3 and the server has python2.7 set as default, your scripts will all fail in very interesting ways.

    – BoboDarph
    Jan 3 at 14:44











  • removed irrelevant "django" and "drf" tags.

    – bruno desthuilliers
    Jan 3 at 14:55

















The second approach leaves you at the mercy of whatever the default python interpreter on the executing machine is. Say you wrote code for python3 and the server has python2.7 set as default, your scripts will all fail in very interesting ways.

– BoboDarph
Jan 3 at 14:44





The second approach leaves you at the mercy of whatever the default python interpreter on the executing machine is. Say you wrote code for python3 and the server has python2.7 set as default, your scripts will all fail in very interesting ways.

– BoboDarph
Jan 3 at 14:44













removed irrelevant "django" and "drf" tags.

– bruno desthuilliers
Jan 3 at 14:55





removed irrelevant "django" and "drf" tags.

– bruno desthuilliers
Jan 3 at 14:55












2 Answers
2






active

oldest

votes


















0















I'm planning to build a WebApp that will need to execute scripts




Why should it "execute scripts" ? Turn your "scripts" into proper modules, import the relevant functions and call them. The fact that Python can be used as a "scripting language" doesn't mean it's not a proper programming language.






share|improve this answer
























  • yes I think that's what I'm going to do, Is that a standard practice in django?

    – Arximede
    Jan 3 at 15:04











  • Also, do you have some tips on organizing the modules? should I have one folder, and all my modules go there?

    – Arximede
    Jan 3 at 15:06











  • The "standard practice" in django is to use Python standard practices - Django is a python framework, so it's just python code in the end. And "how to organize your modules" is way too broad a question - it really depends on what your code is doing. The only hints are the usual "high cohesion / low coupling".

    – bruno desthuilliers
    Jan 3 at 15:13



















-1














Approach (1) should be the default approach. Never subprocess unless you absolutely have to.



Disadvantages of subprocessing:




  1. Depends on the underlying OS and in your case Python (i.e. is python command the same as the Python that runs the original script?).

  2. Potentially harder to make safe.

  3. Harder to pass values, return results and report errors.

  4. Eats more memory and cpu (a side effect is that you can utilize all cpu cores but since you are writing a web app it is likely you do that anyway).

  5. Generally harder to code and maintain.


Advantages of subprocessing:




  1. Isolates the runtime. This is useful if for example scripts are uploaded by users. You don't want them to mess with your application.

  2. Related to 1: potentially easier to dynamically add scripts. Not that you should do that anyway. Also becomes harder when you have more then 1 server and you need to synchronize them.

  3. Well, you can run non-python code that way. But it doesn't apply to your case.






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',
    autoActivateHeartbeat: false,
    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%2f54024431%2fwhat-is-the-recommended-practice-in-django-to-execute-external-scripts%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









    0















    I'm planning to build a WebApp that will need to execute scripts




    Why should it "execute scripts" ? Turn your "scripts" into proper modules, import the relevant functions and call them. The fact that Python can be used as a "scripting language" doesn't mean it's not a proper programming language.






    share|improve this answer
























    • yes I think that's what I'm going to do, Is that a standard practice in django?

      – Arximede
      Jan 3 at 15:04











    • Also, do you have some tips on organizing the modules? should I have one folder, and all my modules go there?

      – Arximede
      Jan 3 at 15:06











    • The "standard practice" in django is to use Python standard practices - Django is a python framework, so it's just python code in the end. And "how to organize your modules" is way too broad a question - it really depends on what your code is doing. The only hints are the usual "high cohesion / low coupling".

      – bruno desthuilliers
      Jan 3 at 15:13
















    0















    I'm planning to build a WebApp that will need to execute scripts




    Why should it "execute scripts" ? Turn your "scripts" into proper modules, import the relevant functions and call them. The fact that Python can be used as a "scripting language" doesn't mean it's not a proper programming language.






    share|improve this answer
























    • yes I think that's what I'm going to do, Is that a standard practice in django?

      – Arximede
      Jan 3 at 15:04











    • Also, do you have some tips on organizing the modules? should I have one folder, and all my modules go there?

      – Arximede
      Jan 3 at 15:06











    • The "standard practice" in django is to use Python standard practices - Django is a python framework, so it's just python code in the end. And "how to organize your modules" is way too broad a question - it really depends on what your code is doing. The only hints are the usual "high cohesion / low coupling".

      – bruno desthuilliers
      Jan 3 at 15:13














    0












    0








    0








    I'm planning to build a WebApp that will need to execute scripts




    Why should it "execute scripts" ? Turn your "scripts" into proper modules, import the relevant functions and call them. The fact that Python can be used as a "scripting language" doesn't mean it's not a proper programming language.






    share|improve this answer














    I'm planning to build a WebApp that will need to execute scripts




    Why should it "execute scripts" ? Turn your "scripts" into proper modules, import the relevant functions and call them. The fact that Python can be used as a "scripting language" doesn't mean it's not a proper programming language.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 3 at 14:54









    bruno desthuilliersbruno desthuilliers

    51.7k54465




    51.7k54465













    • yes I think that's what I'm going to do, Is that a standard practice in django?

      – Arximede
      Jan 3 at 15:04











    • Also, do you have some tips on organizing the modules? should I have one folder, and all my modules go there?

      – Arximede
      Jan 3 at 15:06











    • The "standard practice" in django is to use Python standard practices - Django is a python framework, so it's just python code in the end. And "how to organize your modules" is way too broad a question - it really depends on what your code is doing. The only hints are the usual "high cohesion / low coupling".

      – bruno desthuilliers
      Jan 3 at 15:13



















    • yes I think that's what I'm going to do, Is that a standard practice in django?

      – Arximede
      Jan 3 at 15:04











    • Also, do you have some tips on organizing the modules? should I have one folder, and all my modules go there?

      – Arximede
      Jan 3 at 15:06











    • The "standard practice" in django is to use Python standard practices - Django is a python framework, so it's just python code in the end. And "how to organize your modules" is way too broad a question - it really depends on what your code is doing. The only hints are the usual "high cohesion / low coupling".

      – bruno desthuilliers
      Jan 3 at 15:13

















    yes I think that's what I'm going to do, Is that a standard practice in django?

    – Arximede
    Jan 3 at 15:04





    yes I think that's what I'm going to do, Is that a standard practice in django?

    – Arximede
    Jan 3 at 15:04













    Also, do you have some tips on organizing the modules? should I have one folder, and all my modules go there?

    – Arximede
    Jan 3 at 15:06





    Also, do you have some tips on organizing the modules? should I have one folder, and all my modules go there?

    – Arximede
    Jan 3 at 15:06













    The "standard practice" in django is to use Python standard practices - Django is a python framework, so it's just python code in the end. And "how to organize your modules" is way too broad a question - it really depends on what your code is doing. The only hints are the usual "high cohesion / low coupling".

    – bruno desthuilliers
    Jan 3 at 15:13





    The "standard practice" in django is to use Python standard practices - Django is a python framework, so it's just python code in the end. And "how to organize your modules" is way too broad a question - it really depends on what your code is doing. The only hints are the usual "high cohesion / low coupling".

    – bruno desthuilliers
    Jan 3 at 15:13













    -1














    Approach (1) should be the default approach. Never subprocess unless you absolutely have to.



    Disadvantages of subprocessing:




    1. Depends on the underlying OS and in your case Python (i.e. is python command the same as the Python that runs the original script?).

    2. Potentially harder to make safe.

    3. Harder to pass values, return results and report errors.

    4. Eats more memory and cpu (a side effect is that you can utilize all cpu cores but since you are writing a web app it is likely you do that anyway).

    5. Generally harder to code and maintain.


    Advantages of subprocessing:




    1. Isolates the runtime. This is useful if for example scripts are uploaded by users. You don't want them to mess with your application.

    2. Related to 1: potentially easier to dynamically add scripts. Not that you should do that anyway. Also becomes harder when you have more then 1 server and you need to synchronize them.

    3. Well, you can run non-python code that way. But it doesn't apply to your case.






    share|improve this answer






























      -1














      Approach (1) should be the default approach. Never subprocess unless you absolutely have to.



      Disadvantages of subprocessing:




      1. Depends on the underlying OS and in your case Python (i.e. is python command the same as the Python that runs the original script?).

      2. Potentially harder to make safe.

      3. Harder to pass values, return results and report errors.

      4. Eats more memory and cpu (a side effect is that you can utilize all cpu cores but since you are writing a web app it is likely you do that anyway).

      5. Generally harder to code and maintain.


      Advantages of subprocessing:




      1. Isolates the runtime. This is useful if for example scripts are uploaded by users. You don't want them to mess with your application.

      2. Related to 1: potentially easier to dynamically add scripts. Not that you should do that anyway. Also becomes harder when you have more then 1 server and you need to synchronize them.

      3. Well, you can run non-python code that way. But it doesn't apply to your case.






      share|improve this answer




























        -1












        -1








        -1







        Approach (1) should be the default approach. Never subprocess unless you absolutely have to.



        Disadvantages of subprocessing:




        1. Depends on the underlying OS and in your case Python (i.e. is python command the same as the Python that runs the original script?).

        2. Potentially harder to make safe.

        3. Harder to pass values, return results and report errors.

        4. Eats more memory and cpu (a side effect is that you can utilize all cpu cores but since you are writing a web app it is likely you do that anyway).

        5. Generally harder to code and maintain.


        Advantages of subprocessing:




        1. Isolates the runtime. This is useful if for example scripts are uploaded by users. You don't want them to mess with your application.

        2. Related to 1: potentially easier to dynamically add scripts. Not that you should do that anyway. Also becomes harder when you have more then 1 server and you need to synchronize them.

        3. Well, you can run non-python code that way. But it doesn't apply to your case.






        share|improve this answer















        Approach (1) should be the default approach. Never subprocess unless you absolutely have to.



        Disadvantages of subprocessing:




        1. Depends on the underlying OS and in your case Python (i.e. is python command the same as the Python that runs the original script?).

        2. Potentially harder to make safe.

        3. Harder to pass values, return results and report errors.

        4. Eats more memory and cpu (a side effect is that you can utilize all cpu cores but since you are writing a web app it is likely you do that anyway).

        5. Generally harder to code and maintain.


        Advantages of subprocessing:




        1. Isolates the runtime. This is useful if for example scripts are uploaded by users. You don't want them to mess with your application.

        2. Related to 1: potentially easier to dynamically add scripts. Not that you should do that anyway. Also becomes harder when you have more then 1 server and you need to synchronize them.

        3. Well, you can run non-python code that way. But it doesn't apply to your case.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Jan 3 at 15:16

























        answered Jan 3 at 14:56









        freakishfreakish

        39.8k695139




        39.8k695139






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54024431%2fwhat-is-the-recommended-practice-in-django-to-execute-external-scripts%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

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas