calling previously writen python scripts [How to use import]

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick C
    New Member
    • Apr 2007
    • 54

    calling previously writen python scripts [How to use import]

    Python world,

    I've writen a simple script and saved in text editor as a .py file. I'd like to now call that script within a new scrip i've written (so that I dont ahve to retype the whole content of the previously saved .py file)

    What's the command for that?

    Thanks

    Pat, the simpleton.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Patrick C
    Python world,

    I've writen a simple script and saved in text editor as a .py file. I'd like to now call that script within a new scrip i've written (so that I dont ahve to retype the whole content of the previously saved .py file)

    What's the command for that?

    Thanks

    Pat, the simpleton.
    You can use import. Import runs the selected module and all names from that module are accessable. Would that work for you?

    Comment

    • bartonc
      Recognized Expert Expert
      • Sep 2006
      • 6478

      #3
      Originally posted by Patrick C
      Python world,

      I've writen a simple script and saved in text editor as a .py file. I'd like to now call that script within a new scrip i've written (so that I dont ahve to retype the whole content of the previously saved .py file)

      What's the command for that?

      Thanks

      Pat, the simpleton.
      As my friend, ilikepython, says: import is the key:
      Code:
      import MyModule
      MyModule.AnyFunction(anyArgs)
      is the cleanest (for the namespace of the importing module). NOTE: Leave off the .py.
      Code:
      from MyModule import AnyFunction
      AnyFuncion(anyArgs)
      is the one that I use most. It's very common to see things like:
      Code:
      from time import time
      print time()
      A third option (which is discouraged because it clutters the namespace of the importing module) is:
      Code:
      form MyModule import *
      AnyFunction() #in MyModule
      Note that import actually runs all the commands in the module being imported. So, if it's written as a "script" (in-line statements without function defs) you don't need to call any functions to make it run. Simply
      Code:
      import MyModule

      Comment

      • Patrick C
        New Member
        • Apr 2007
        • 54

        #4
        Originally posted by ilikepython
        You can use import. Import runs the selected module and all names from that module are accessable. Would that work for you?
        Sorry I should clarify,

        I have that much, but in the script i wrote, has functions I want to access. How would I access those functions, or should they already be there?

        For example my script ends with a print statment. Now I don't want to do all th work of writing the script so I just say

        Import previousScripty
        print DefinedFunction

        (or maybe i'm doing that part wrong, would i need import previousScript. py ?)

        thanks

        Comment

        • Patrick C
          New Member
          • Apr 2007
          • 54

          #5
          Thanks again guys

          So if i'm clear here...
          lets say I have a file named AAA.py
          in AAA i have a a defined function that goes
          def BBB():
          blah blah blah

          So now in a new script i can say...

          Import AAA
          From AAA import BBB ?

          Thanks
          PC

          Originally posted by bartonc
          As my friend, ilikepython, says: import is the key:
          Code:
          import MyModule
          MyModule.AnyFunction(anyArgs)
          is the cleanest (for the namespace of the importing module). NOTE: Leave off the .py.
          Code:
          from MyModule import AnyFunction
          AnyFuncion(anyArgs)
          is the one that I use most. It's very common to see things like:
          Code:
          from time import time
          print time()
          A third option (which is discouraged because it clutters the namespace of the importing module) is:
          Code:
          form MyModule import *
          AnyFunction() #in MyModule
          Note that import actually runs all the commands in the module being imported. So, if it's written as a "script" (in-line statements without function defs) you don't need to call any functions to make it run. Simply
          Code:
          import MyModule

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by Patrick C
            Thanks again guys

            So if i'm clear here...
            lets say I have a file named AAA.py
            in AAA i have a a defined function that goes
            def BBB():
            blah blah blah

            So now in a new script i can say...

            Import AAA
            From AAA import BBB ?

            Thanks
            PC
            Yes, that is exactly the way. Then you would just call the function like "BBB()".Exc ept remember that you don't need the first import statement if you are only using that one function.

            Comment

            • bartonc
              Recognized Expert Expert
              • Sep 2006
              • 6478

              #7
              Originally posted by Patrick C
              Thanks again guys

              So if i'm clear here...
              lets say I have a file named AAA.py
              in AAA i have a a defined function that goes
              def BBB():
              blah blah blah

              So now in a new script i can say...

              Import AAA
              From AAA import BBB ?

              Thanks
              PC
              As my friend says: Use either
              Code:
              Import AAA
              or
              Code:
              From AAA import BBB
              Not both.

              Comment

              Working...