how to access a variable from a .py script in another .py script










-2















I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!



first.py



def main():
results = os.path.abspath(r'D:results')
return 0


second.py



-> need to access results here


Any help highly appreciated?










share|improve this question



















  • 1





    Something like this, but bear in mind you are returning 0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).

    – Peter
    Nov 12 '18 at 11:57












  • results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.

    – Mike Scotty
    Nov 12 '18 at 11:58












  • You might want to start using pickle that allows you to save and load python objects to/from disk

    – jojo
    Nov 12 '18 at 12:06
















-2















I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!



first.py



def main():
results = os.path.abspath(r'D:results')
return 0


second.py



-> need to access results here


Any help highly appreciated?










share|improve this question



















  • 1





    Something like this, but bear in mind you are returning 0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).

    – Peter
    Nov 12 '18 at 11:57












  • results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.

    – Mike Scotty
    Nov 12 '18 at 11:58












  • You might want to start using pickle that allows you to save and load python objects to/from disk

    – jojo
    Nov 12 '18 at 12:06














-2












-2








-2








I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!



first.py



def main():
results = os.path.abspath(r'D:results')
return 0


second.py



-> need to access results here


Any help highly appreciated?










share|improve this question
















I need to get the updated value of a variable(ex: results) , which is set in the main() function of a first.py script in the second.py script!



first.py



def main():
results = os.path.abspath(r'D:results')
return 0


second.py



-> need to access results here


Any help highly appreciated?







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 12:00









Mike Scotty

5,53751933




5,53751933










asked Nov 12 '18 at 11:55









Remya ReveendranRemya Reveendran

11




11







  • 1





    Something like this, but bear in mind you are returning 0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).

    – Peter
    Nov 12 '18 at 11:57












  • results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.

    – Mike Scotty
    Nov 12 '18 at 11:58












  • You might want to start using pickle that allows you to save and load python objects to/from disk

    – jojo
    Nov 12 '18 at 12:06













  • 1





    Something like this, but bear in mind you are returning 0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).

    – Peter
    Nov 12 '18 at 11:57












  • results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.

    – Mike Scotty
    Nov 12 '18 at 11:58












  • You might want to start using pickle that allows you to save and load python objects to/from disk

    – jojo
    Nov 12 '18 at 12:06








1




1





Something like this, but bear in mind you are returning 0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).

– Peter
Nov 12 '18 at 11:57






Something like this, but bear in mind you are returning 0, not result_path - from first import main; var = main(). If you need to access multiple variables inside the function, you could use a class instead and set them as attributes (from first import Main; cls = Main(); var = cls.result_path).

– Peter
Nov 12 '18 at 11:57














results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.

– Mike Scotty
Nov 12 '18 at 11:58






results is only known within the scope of main(), you cannot access it from anywhere else, not even from first.py.

– Mike Scotty
Nov 12 '18 at 11:58














You might want to start using pickle that allows you to save and load python objects to/from disk

– jojo
Nov 12 '18 at 12:06






You might want to start using pickle that allows you to save and load python objects to/from disk

– jojo
Nov 12 '18 at 12:06













4 Answers
4






active

oldest

votes


















0














You will need a function, which will then be imported, like:



from example.py import func()


Hope this helps.






share|improve this answer






























    0














    In the file second.py



    import first as f

    main = f.main()
    result = main.result_path


    Please try this. I hope it helps a bit.






    share|improve this answer


















    • 1





      main will be the int value 0, so you're trying to access 0.result_path, which will fail.

      – Mike Scotty
      Nov 12 '18 at 12:05











    • in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.

      – Alex_P
      Nov 12 '18 at 12:14


















    0














    You need to import your first script in your second and additionally set result_path as a global variable.



    first.py:



    result_path = None

    def main():
    global result_path
    result_path = os.path.abspath(r'D:results')


    second.py:



    import first

    access = first.result_path


    You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.






    share|improve this answer























    • Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()

      – Remya Reveendran
      Nov 12 '18 at 12:47



















    0














    This will work



    First.py



    def main():
    results = os.path.abspath(r'D:results')
    return results


    second.py



    from First import main
    resultsHere = main()





    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%2f53261653%2fhow-to-access-a-variable-from-a-py-script-in-another-py-script%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      You will need a function, which will then be imported, like:



      from example.py import func()


      Hope this helps.






      share|improve this answer



























        0














        You will need a function, which will then be imported, like:



        from example.py import func()


        Hope this helps.






        share|improve this answer

























          0












          0








          0







          You will need a function, which will then be imported, like:



          from example.py import func()


          Hope this helps.






          share|improve this answer













          You will need a function, which will then be imported, like:



          from example.py import func()


          Hope this helps.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 '18 at 11:58









          mgmc2mgmc2

          63




          63























              0














              In the file second.py



              import first as f

              main = f.main()
              result = main.result_path


              Please try this. I hope it helps a bit.






              share|improve this answer


















              • 1





                main will be the int value 0, so you're trying to access 0.result_path, which will fail.

                – Mike Scotty
                Nov 12 '18 at 12:05











              • in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.

                – Alex_P
                Nov 12 '18 at 12:14















              0














              In the file second.py



              import first as f

              main = f.main()
              result = main.result_path


              Please try this. I hope it helps a bit.






              share|improve this answer


















              • 1





                main will be the int value 0, so you're trying to access 0.result_path, which will fail.

                – Mike Scotty
                Nov 12 '18 at 12:05











              • in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.

                – Alex_P
                Nov 12 '18 at 12:14













              0












              0








              0







              In the file second.py



              import first as f

              main = f.main()
              result = main.result_path


              Please try this. I hope it helps a bit.






              share|improve this answer













              In the file second.py



              import first as f

              main = f.main()
              result = main.result_path


              Please try this. I hope it helps a bit.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 '18 at 12:02









              Alex_PAlex_P

              193114




              193114







              • 1





                main will be the int value 0, so you're trying to access 0.result_path, which will fail.

                – Mike Scotty
                Nov 12 '18 at 12:05











              • in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.

                – Alex_P
                Nov 12 '18 at 12:14












              • 1





                main will be the int value 0, so you're trying to access 0.result_path, which will fail.

                – Mike Scotty
                Nov 12 '18 at 12:05











              • in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.

                – Alex_P
                Nov 12 '18 at 12:14







              1




              1





              main will be the int value 0, so you're trying to access 0.result_path, which will fail.

              – Mike Scotty
              Nov 12 '18 at 12:05





              main will be the int value 0, so you're trying to access 0.result_path, which will fail.

              – Mike Scotty
              Nov 12 '18 at 12:05













              in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.

              – Alex_P
              Nov 12 '18 at 12:14





              in this case adjust 'main()' to return result_path. I was more trying to access the variable in the second file and not to correct his first file. For me, as long he can access the file in the second path, the question should be answered. Please correct me if I am wrong.

              – Alex_P
              Nov 12 '18 at 12:14











              0














              You need to import your first script in your second and additionally set result_path as a global variable.



              first.py:



              result_path = None

              def main():
              global result_path
              result_path = os.path.abspath(r'D:results')


              second.py:



              import first

              access = first.result_path


              You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.






              share|improve this answer























              • Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()

                – Remya Reveendran
                Nov 12 '18 at 12:47
















              0














              You need to import your first script in your second and additionally set result_path as a global variable.



              first.py:



              result_path = None

              def main():
              global result_path
              result_path = os.path.abspath(r'D:results')


              second.py:



              import first

              access = first.result_path


              You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.






              share|improve this answer























              • Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()

                – Remya Reveendran
                Nov 12 '18 at 12:47














              0












              0








              0







              You need to import your first script in your second and additionally set result_path as a global variable.



              first.py:



              result_path = None

              def main():
              global result_path
              result_path = os.path.abspath(r'D:results')


              second.py:



              import first

              access = first.result_path


              You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.






              share|improve this answer













              You need to import your first script in your second and additionally set result_path as a global variable.



              first.py:



              result_path = None

              def main():
              global result_path
              result_path = os.path.abspath(r'D:results')


              second.py:



              import first

              access = first.result_path


              You could also define a method in first.py, that just returns result_path and then call it in second.py, but therefor you'd still need to set result_path global.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 12 '18 at 12:07









              AracKnightAracKnight

              387




              387












              • Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()

                – Remya Reveendran
                Nov 12 '18 at 12:47


















              • Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()

                – Remya Reveendran
                Nov 12 '18 at 12:47

















              Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()

              – Remya Reveendran
              Nov 12 '18 at 12:47






              Hi AracKnight Thanks ! this solution works . But currently i am facing another problem . Currently i changed my logic to update the 'result_path' variable dynamically based on the execution of first.py (not a hardcoded path i mean) , in that case I am unable to fetch it in second.py script now ! Stuck again i am calling first.py in second.py using subprocess.popen ()

              – Remya Reveendran
              Nov 12 '18 at 12:47












              0














              This will work



              First.py



              def main():
              results = os.path.abspath(r'D:results')
              return results


              second.py



              from First import main
              resultsHere = main()





              share|improve this answer



























                0














                This will work



                First.py



                def main():
                results = os.path.abspath(r'D:results')
                return results


                second.py



                from First import main
                resultsHere = main()





                share|improve this answer

























                  0












                  0








                  0







                  This will work



                  First.py



                  def main():
                  results = os.path.abspath(r'D:results')
                  return results


                  second.py



                  from First import main
                  resultsHere = main()





                  share|improve this answer













                  This will work



                  First.py



                  def main():
                  results = os.path.abspath(r'D:results')
                  return results


                  second.py



                  from First import main
                  resultsHere = main()






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 '18 at 12:07









                  HayatHayat

                  526415




                  526415



























                      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%2f53261653%2fhow-to-access-a-variable-from-a-py-script-in-another-py-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

                      Kleinkühnau

                      Makov (Slowakei)

                      Deutsches Schauspielhaus