How to call a function defined in another py file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • silverburgh.meryl@gmail.com

    How to call a function defined in another py file

    Hi,

    I have a function called 'test' defined in A.py.
    How can I call that function test in my another file B.py?

    Thank you.

  • Martin Blume

    #2
    Re: How to call a function defined in another py file

    <silverburgh.me ryl@gmail.comsc hrieb
    >
    I have a function called 'test' defined in A.py.
    How can I call that function test in my another file B.py?
    >
    In B.py:

    import A

    A.test()


    HTH
    Martin


    Comment

    • silverburgh.meryl@gmail.com

      #3
      Re: How to call a function defined in another py file

      On Feb 19, 2:22 pm, "Martin Blume" <mbl...@socha.n etwrote:
      <silverburgh.me ...@gmail.comsc hrieb
      >
      I have a function called 'test' defined in A.py.
      How can I call that function test in my another file B.py?
      >
      In B.py:
      >
      import A
      >
      A.test()
      >
      HTH
      Martin
      But Do I need to put A.py and B.py in the same directory?
      if not, where does python look for A.py ?
      And do I need to compile A.py before I can import it to B.py?

      Comment

      • Jeremy Gransden

        #4
        Re: How to call a function defined in another py file

        from a import test


        be sure a is in your path.


        jeremy

        On Feb 19, 2007, at 3:20 PM, silverburgh.mer yl@gmail.com wrote:
        Hi,
        >
        I have a function called 'test' defined in A.py.
        How can I call that function test in my another file B.py?
        >
        Thank you.
        >
        --
        http://mail.python.org/mailman/listinfo/python-list

        Comment

        • Martin Blume

          #5
          Re: How to call a function defined in another py file

          <silverburgh.me ryl@gmail.comsc hrieb
          >>
          >>I have a function called 'test' defined in A.py.
          >>How can I call that function test in my another file B.py?
          >>
          >In B.py:
          > import A
          > A.test()
          >>
          >
          But Do I need to put A.py and B.py in the same directory?
          No, but then you have to take certain precautions. (*)
          if not, where does python look for A.py ?
          In the path defined by the (IIRC) PYTHONPATH (*)
          And do I need to compile A.py before I can import it to B.py?
          No.

          (*) you might want to read the fine documentation at

          which tells it much better than I do, and might give you
          some more ideas for googling.
          I haven't had yet the necessity for cross-directory imports.

          HTH
          Martin



          Comment

          • Bruno Desthuilliers

            #6
            Re: How to call a function defined in another py file

            silverburgh.mer yl@gmail.com a écrit :
            Hi,
            >
            I have a function called 'test' defined in A.py.
            How can I call that function test in my another file B.py?
            >
            Thank you.
            >
            # b.py

            import A
            A.test()

            Comment

            • Jeremy Gransden

              #7
              Re: How to call a function defined in another py file


              On Feb 19, 2007, at 3:27 PM, silverburgh.mer yl@gmail.com wrote:
              On Feb 19, 2:22 pm, "Martin Blume" <mbl...@socha.n etwrote:
              ><silverburgh.m e...@gmail.coms chrieb
              >>
              >>I have a function called 'test' defined in A.py.
              >>How can I call that function test in my another file B.py?
              >>
              >In B.py:
              >>
              >import A
              >>
              >A.test()
              >>
              >HTH
              >Martin
              >
              But Do I need to put A.py and B.py in the same directory?
              if not, where does python look for A.py ?
              No, they do not have to be in the same directory. A.py needs to be in
              your path.
              And do I need to compile A.py before I can import it to B.py?
              No




              jeremy


              Comment

              Working...