import woe

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

    import woe

    hello,

    i have a problem. i would like to import python files above and below
    my current directory.

    i'm working on /home/foo/bar/jar.py

    i would like to import /home/foo/car.py and
    /home/foo/bar/far.py

    how can i do this?

    thank you,
    bob

    ps: i want to scale, so i do not want to edit the python path

  • Gary Herron

    #2
    Re: import woe

    gsocks@gmail.co m wrote:
    [color=blue]
    >hello,
    >
    >i have a problem. i would like to import python files above and below
    >my current directory.
    >
    >i'm working on /home/foo/bar/jar.py
    >
    >i would like to import /home/foo/car.py and
    > /home/foo/bar/far.py
    >
    >how can i do this?
    >
    >thank you,
    >bob
    >
    >ps: i want to scale, so i do not want to edit the python path
    >
    >
    >[/color]
    Work out the path name for the directory from which you'd like to
    import, and append it to sys.path:

    import sys
    sys.path.append ('..')
    sys.path.append ('whatever/path/absolute/or/relative')

    Then off you go importing as you wish.

    Comment

    • Serge Orlov

      #3
      Re: import woe

      gsocks@gmail.co m wrote:[color=blue]
      > hello,
      >
      > i have a problem. i would like to import python files above and below
      > my current directory.
      >
      > i'm working on /home/foo/bar/jar.py
      >
      > i would like to import /home/foo/car.py and
      > /home/foo/bar/far.py
      >
      > how can i do this?[/color]

      $ cat >>~/.bashrc
      export PATH=/home/foo/:$PATH
      $ cat >/home/foo/application
      #!/usr/bin/env python
      import bar.jar
      $ chmod +x /home/foo/application
      $ cd /home/foo/bar
      $ application
      ..... all imports work fine ...
      [color=blue]
      > ps: i want to scale, so i do not want to edit the python path[/color]

      In what sense do you want to scale, working with multiple projects or
      multiple versions of one project at the same time? Anyway you are to
      quick to jump to conclusions, if you don't want to edit python path who
      will do it for you? Python path won't appear out of thin air if your
      file layout is not supported out of the box.

      Comment

      • vaibhav

        #4
        Re: import woe

        Hi bob,

        1. decide the directory which will be your root folder containing foo
        [/home/ROOT/foo/]

        2. work out your directory structure relative to this root folder
        here it is ->ROOT->foo->car.py
        ->bar->far.py
        ->bar->jar.py

        3. add __init__.py file inside each folder containing a list variable
        __all__ with contents as the name of the directories and classes
        so foo folder should contain a file called __init__.py which has the
        following contents
        __all__ = ['bar','car']
        and bar folder should contain a file called __init__.py which has the
        following contents
        __all__ = ['far','jar']

        4. add the root folder to your sys.path
        so your jar.py file should have the following entries
        from sys import path
        path.append('../../../ROOT')

        note: i prefer relative paths or make paths using os.getcwd
        combinations in such situations, which makes the class more flexible.
        you can also do this step where u configure/initialize

        5. then you can import the classes you want in jar.py

        from foo import car
        from foo.bar import far

        pls mail if u dont get it working/any doubts.

        -
        vaibhav

        Comment

        • Terry Hancock

          #5
          Re: import woe

          vaibhav wrote:
          [color=blue]
          >4. add the root folder to your sys.path
          >so your jar.py file should have the following entries
          >from sys import path
          >path.append('. ./../../ROOT')
          >
          >note: i prefer relative paths
          >
          >[/color]
          Interesting that that works. I guess you could create
          a limited form of Zope acquisition type behavior:

          sys.path = ['.', '../', '../../', '../../../'] + sys.path

          I'm looking forward to the introduction of relative
          imports in 2.5, though.

          Cheers,
          Terry

          --
          Terry Hancock (hancock@Anansi Spaceworks.com)
          Anansi Spaceworks http://www.AnansiSpaceworks.com


          Comment

          Working...