How to - import code not in current directory

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • py

    #1

    How to - import code not in current directory

    I have a python script that I want to test/debug. It contains a class
    which extends from some other class which is located in some other
    python file in a different directory.

    For example:

    [script to test]
    c:\python_code\ foo.py

    [needed python files]
    c:\some\other\d irectory\bar.py

    ....so I want to test/debug foo.py, which needs bar.py. foo.py imports
    bar.py ...but in order to test out foo (in the python shell) I normally
    copy bar.py into the same directory as foo.py...but this is painful.
    Is there some way that I can have python know about the directory where
    bar.py is located, like a system variable, etc? If so, how do I set
    that up?

    Thanks.

  • Claudio Grondi

    #2
    Re: How to - import code not in current directory

    This question seems to come up in this newsgroup quite often, so looking
    through past threads will sure provide more details.

    Here from "Re: how to import a module from a arbitraty path?"
    posted to comp.lang.pytho n by Simon Brunning on May 26, 2005 09:20 :

    "[color=blue]
    > I have a program which is going to dynamicly load components from some
    > arbitrary defined paths. How to do that?[/color]

    You can locate them with os.walk and fnmatch. Then you can temporarily
    add the directory to sys.path, and import using __import__().
    "

    so this should work in your case:

    import sys
    sys.path.append ("C:\some\other \directory")
    import bar

    Claudio


    "py" <codecraig@gmai l.com> schrieb im Newsbeitrag
    news:1132233527 .222443.139470@ g14g2000cwa.goo glegroups.com.. .[color=blue]
    > I have a python script that I want to test/debug. It contains a class
    > which extends from some other class which is located in some other
    > python file in a different directory.
    >
    > For example:
    >
    > [script to test]
    > c:\python_code\ foo.py
    >
    > [needed python files]
    > c:\some\other\d irectory\bar.py
    >
    > ...so I want to test/debug foo.py, which needs bar.py. foo.py imports
    > bar.py ...but in order to test out foo (in the python shell) I normally
    > copy bar.py into the same directory as foo.py...but this is painful.
    > Is there some way that I can have python know about the directory where
    > bar.py is located, like a system variable, etc? If so, how do I set
    > that up?
    >
    > Thanks.
    >[/color]


    Comment

    • Claudio Grondi

      #3
      Re: How to - import code not in current directory

      This question seems to come up in this newsgroup quite often, so looking
      through past threads will sure provide more details.

      Here from "Re: how to import a module from a arbitraty path?"
      posted to comp.lang.pytho n by Simon Brunning on May 26, 2005 09:20 :

      "[color=blue]
      > I have a program which is going to dynamicly load components from some
      > arbitrary defined paths. How to do that?[/color]

      You can locate them with os.walk and fnmatch. Then you can temporarily
      add the directory to sys.path, and import using __import__().
      "

      so this should work in your case:

      import sys
      sys.path.append ("C:\some\other \directory")
      import bar

      Claudio


      "py" <codecraig@gmai l.com> schrieb im Newsbeitrag
      news:1132233527 .222443.139470@ g14g2000cwa.goo glegroups.com.. .[color=blue]
      > I have a python script that I want to test/debug. It contains a class
      > which extends from some other class which is located in some other
      > python file in a different directory.
      >
      > For example:
      >
      > [script to test]
      > c:\python_code\ foo.py
      >
      > [needed python files]
      > c:\some\other\d irectory\bar.py
      >
      > ...so I want to test/debug foo.py, which needs bar.py. foo.py imports
      > bar.py ...but in order to test out foo (in the python shell) I normally
      > copy bar.py into the same directory as foo.py...but this is painful.
      > Is there some way that I can have python know about the directory where
      > bar.py is located, like a system variable, etc? If so, how do I set
      > that up?
      >
      > Thanks.
      >[/color]


      Comment

      • py

        #4
        Re: How to - import code not in current directory

        Claudio Grondi wrote:[color=blue]
        > so this should work in your case:
        >
        > import sys
        > sys.path.append ("C:\some\other \directory")
        > import bar[/color]

        ....that will certainly work. Only issue is that each time I start up
        foo.py in the python shell I have to retype those three lines....kind
        of why I was hoping for a environment variable or path setting that i
        could stick in.

        This will do for now...

        Comment

        • py

          #5
          Re: How to - import code not in current directory

          Claudio Grondi wrote:[color=blue]
          > so this should work in your case:
          >
          > import sys
          > sys.path.append ("C:\some\other \directory")
          > import bar[/color]

          ....that will certainly work. Only issue is that each time I start up
          foo.py in the python shell I have to retype those three lines....kind
          of why I was hoping for a environment variable or path setting that i
          could stick in.

          This will do for now...

          Comment

          • Carsten Haese

            #6
            Re: How to - import code not in current directory

            On Thu, 2005-11-17 at 08:41, py wrote:[color=blue]
            > Claudio Grondi wrote:[color=green]
            > > so this should work in your case:
            > >
            > > import sys
            > > sys.path.append ("C:\some\other \directory")
            > > import bar[/color]
            >
            > ...that will certainly work. Only issue is that each time I start up
            > foo.py in the python shell I have to retype those three lines....kind
            > of why I was hoping for a environment variable or path setting that i
            > could stick in.[/color]

            That would be PYTHONPATH.

            -Carsten


            Comment

            • Carsten Haese

              #7
              Re: How to - import code not in current directory

              On Thu, 2005-11-17 at 08:41, py wrote:[color=blue]
              > Claudio Grondi wrote:[color=green]
              > > so this should work in your case:
              > >
              > > import sys
              > > sys.path.append ("C:\some\other \directory")
              > > import bar[/color]
              >
              > ...that will certainly work. Only issue is that each time I start up
              > foo.py in the python shell I have to retype those three lines....kind
              > of why I was hoping for a environment variable or path setting that i
              > could stick in.[/color]

              That would be PYTHONPATH.

              -Carsten


              Comment

              • Claudio Grondi

                #8
                Re: How to - import code not in current directory

                "py" <codecraig@gmai l.com> schrieb im Newsbeitrag
                news:1132234860 .961767.209600@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                > Claudio Grondi wrote:[color=green]
                > > so this should work in your case:
                > >
                > > import sys
                > > sys.path.append ("C:\some\other \directory")
                > > import bar[/color]
                >
                > ...that will certainly work. Only issue is that each time I start up
                > foo.py in the python shell I have to retype those three lines....kind
                > of why I was hoping for a environment variable or path setting that i
                > could stick in.
                >
                > This will do for now...
                >[/color]
                See the recent threads
                "IDLE question" and
                "newbie - How do I import automatically?"
                in THIS newsgroup for further help.

                Claudio


                Comment

                • Claudio Grondi

                  #9
                  Re: How to - import code not in current directory

                  "py" <codecraig@gmai l.com> schrieb im Newsbeitrag
                  news:1132234860 .961767.209600@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                  > Claudio Grondi wrote:[color=green]
                  > > so this should work in your case:
                  > >
                  > > import sys
                  > > sys.path.append ("C:\some\other \directory")
                  > > import bar[/color]
                  >
                  > ...that will certainly work. Only issue is that each time I start up
                  > foo.py in the python shell I have to retype those three lines....kind
                  > of why I was hoping for a environment variable or path setting that i
                  > could stick in.
                  >
                  > This will do for now...
                  >[/color]
                  See the recent threads
                  "IDLE question" and
                  "newbie - How do I import automatically?"
                  in THIS newsgroup for further help.

                  Claudio


                  Comment

                  • py

                    #10
                    Re: How to - import code not in current directory

                    PYTHONPATH is perfect....chec k out this link for more info..



                    I just added the environment variable (on windows) named "PYTHONPATH "
                    and set it to "C:\some\other\ directory"

                    :)

                    Comment

                    • py

                      #11
                      Re: How to - import code not in current directory

                      PYTHONPATH is perfect....chec k out this link for more info..



                      I just added the environment variable (on windows) named "PYTHONPATH "
                      and set it to "C:\some\other\ directory"

                      :)

                      Comment

                      Working...