Python - Include another Python script












1















I have a question. When you are programming in PHP you can use this to include external php script to current script.



include('test_page.php');


So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?










share|improve this question

























  • you can use import

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:35











  • Thats for modules, or does it also work with scripts?

    – Vreesie
    Nov 24 '18 at 10:35











  • modules are just fancy scripts. import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:37













  • so I have to use for example: import test_script

    – Vreesie
    Nov 24 '18 at 10:38








  • 1





    aw, arbazz was faster than me with answering :p

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:38
















1















I have a question. When you are programming in PHP you can use this to include external php script to current script.



include('test_page.php');


So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?










share|improve this question

























  • you can use import

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:35











  • Thats for modules, or does it also work with scripts?

    – Vreesie
    Nov 24 '18 at 10:35











  • modules are just fancy scripts. import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:37













  • so I have to use for example: import test_script

    – Vreesie
    Nov 24 '18 at 10:38








  • 1





    aw, arbazz was faster than me with answering :p

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:38














1












1








1








I have a question. When you are programming in PHP you can use this to include external php script to current script.



include('test_page.php');


So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?










share|improve this question
















I have a question. When you are programming in PHP you can use this to include external php script to current script.



include('test_page.php');


So you don't have to repeat a code in every script. Is there a way in Python to include another Python script, just like the php page?







php python include






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 11:12









Arbazz Hussain

144114




144114










asked Nov 24 '18 at 10:34









VreesieVreesie

6212




6212













  • you can use import

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:35











  • Thats for modules, or does it also work with scripts?

    – Vreesie
    Nov 24 '18 at 10:35











  • modules are just fancy scripts. import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:37













  • so I have to use for example: import test_script

    – Vreesie
    Nov 24 '18 at 10:38








  • 1





    aw, arbazz was faster than me with answering :p

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:38



















  • you can use import

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:35











  • Thats for modules, or does it also work with scripts?

    – Vreesie
    Nov 24 '18 at 10:35











  • modules are just fancy scripts. import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:37













  • so I have to use for example: import test_script

    – Vreesie
    Nov 24 '18 at 10:38








  • 1





    aw, arbazz was faster than me with answering :p

    – Tilman B. aka Nerdyyy
    Nov 24 '18 at 10:38

















you can use import

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35





you can use import

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:35













Thats for modules, or does it also work with scripts?

– Vreesie
Nov 24 '18 at 10:35





Thats for modules, or does it also work with scripts?

– Vreesie
Nov 24 '18 at 10:35













modules are just fancy scripts. import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37







modules are just fancy scripts. import xy runs all code in xy.py, including but not limited to: creating methods, creating variables, method calls, etc

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:37















so I have to use for example: import test_script

– Vreesie
Nov 24 '18 at 10:38







so I have to use for example: import test_script

– Vreesie
Nov 24 '18 at 10:38






1




1





aw, arbazz was faster than me with answering :p

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38





aw, arbazz was faster than me with answering :p

– Tilman B. aka Nerdyyy
Nov 24 '18 at 10:38












2 Answers
2






active

oldest

votes


















1














In Python,



you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.



If modulename.py is an function




import modulename




if modulename.py is a set a different functions.




from modulename import particularfunction







share|improve this answer































    0














    If you have "script.py", just make:



    import script


    If you have a particular function or a class in the script then:



    from script import function


    Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:



    import sys
    sys.path.append("PATH_TO_SCRIPT")

    import script





    share|improve this answer
























    • If I want to make a database connection in an include, I get the error: if not self._cnx: ReferenceError: weakly-referenced object no longer exists

      – Vreesie
      Nov 24 '18 at 10:58











    • What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.

      – artona
      Nov 24 '18 at 11:09











    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%2f53457258%2fpython-include-another-python-script%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









    1














    In Python,



    you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.



    If modulename.py is an function




    import modulename




    if modulename.py is a set a different functions.




    from modulename import particularfunction







    share|improve this answer




























      1














      In Python,



      you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.



      If modulename.py is an function




      import modulename




      if modulename.py is a set a different functions.




      from modulename import particularfunction







      share|improve this answer


























        1












        1








        1







        In Python,



        you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.



        If modulename.py is an function




        import modulename




        if modulename.py is a set a different functions.




        from modulename import particularfunction







        share|improve this answer













        In Python,



        you can import the different scripts/modules by using import , just make sure they are in same directory and proper function/class.



        If modulename.py is an function




        import modulename




        if modulename.py is a set a different functions.




        from modulename import particularfunction








        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 10:38









        Arbazz HussainArbazz Hussain

        144114




        144114

























            0














            If you have "script.py", just make:



            import script


            If you have a particular function or a class in the script then:



            from script import function


            Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:



            import sys
            sys.path.append("PATH_TO_SCRIPT")

            import script





            share|improve this answer
























            • If I want to make a database connection in an include, I get the error: if not self._cnx: ReferenceError: weakly-referenced object no longer exists

              – Vreesie
              Nov 24 '18 at 10:58











            • What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.

              – artona
              Nov 24 '18 at 11:09
















            0














            If you have "script.py", just make:



            import script


            If you have a particular function or a class in the script then:



            from script import function


            Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:



            import sys
            sys.path.append("PATH_TO_SCRIPT")

            import script





            share|improve this answer
























            • If I want to make a database connection in an include, I get the error: if not self._cnx: ReferenceError: weakly-referenced object no longer exists

              – Vreesie
              Nov 24 '18 at 10:58











            • What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.

              – artona
              Nov 24 '18 at 11:09














            0












            0








            0







            If you have "script.py", just make:



            import script


            If you have a particular function or a class in the script then:



            from script import function


            Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:



            import sys
            sys.path.append("PATH_TO_SCRIPT")

            import script





            share|improve this answer













            If you have "script.py", just make:



            import script


            If you have a particular function or a class in the script then:



            from script import function


            Remember having it in the pwd. If the script is not in pwd, then you need to append a path to it. So before importing:



            import sys
            sys.path.append("PATH_TO_SCRIPT")

            import script






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 24 '18 at 10:38









            artonaartona

            71247




            71247













            • If I want to make a database connection in an include, I get the error: if not self._cnx: ReferenceError: weakly-referenced object no longer exists

              – Vreesie
              Nov 24 '18 at 10:58











            • What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.

              – artona
              Nov 24 '18 at 11:09



















            • If I want to make a database connection in an include, I get the error: if not self._cnx: ReferenceError: weakly-referenced object no longer exists

              – Vreesie
              Nov 24 '18 at 10:58











            • What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.

              – artona
              Nov 24 '18 at 11:09

















            If I want to make a database connection in an include, I get the error: if not self._cnx: ReferenceError: weakly-referenced object no longer exists

            – Vreesie
            Nov 24 '18 at 10:58





            If I want to make a database connection in an include, I get the error: if not self._cnx: ReferenceError: weakly-referenced object no longer exists

            – Vreesie
            Nov 24 '18 at 10:58













            What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.

            – artona
            Nov 24 '18 at 11:09





            What type of the database do you have? And how do you create this connection? Normally you create connection in the main script, via methods provided with other imported modules and scripts.

            – artona
            Nov 24 '18 at 11:09


















            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%2f53457258%2fpython-include-another-python-script%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

            Futebolista

            Feedback on college project

            Albești (Vaslui)