Relative paths in mod_python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivo van der Sangen

    #1

    Relative paths in mod_python

    I was wondering if I could use relative paths in a mod_python script. At
    the moment I am defining a constant string
    "/path/to/dir/where/script/resides". The problem with this is that when
    I move the script including files I use to get metadata I have to change
    this variable. The same problem occurs when I distribute the script; the
    person using it will first have to modify it, which is not what I want. Is
    it possible that a mod_python script has as current working directory
    the directory where the script resides? At the moment os.getcwd()
    returns '/'.
  • Jochem Berndsen

    #2
    Re: Relative paths in mod_python

    Ivo van der Sangen wrote:[color=blue]
    > I was wondering if I could use relative paths in a mod_python script. At
    > the moment I am defining a constant string
    > "/path/to/dir/where/script/resides".
    > [snip][/color]

    You can use Python's built-in __file__ variable. It is set to the absolute
    location of your script. You can then use one of the built-in functions in
    the `os' module (I forgot which) to extract the directory your script
    resides.

    Jochem.

    --
    E-mail address encrypted using ROT13.

    Comment

    • Bruno Desthuilliers

      #3
      Re: Relative paths in mod_python

      Ivo van der Sangen a écrit :[color=blue]
      > I was wondering if I could use relative paths in a mod_python script. At
      > the moment I am defining a constant string
      > "/path/to/dir/where/script/resides". The problem with this is that when
      > I move the script including files I use to get metadata I have to change
      > this variable. The same problem occurs when I distribute the script; the
      > person using it will first have to modify it, which is not what I want. Is
      > it possible that a mod_python script has as current working directory
      > the directory where the script resides? At the moment os.getcwd()
      > returns '/'.[/color]

      You could set this constant in the apache conf as a PythonOption. And/Or
      you could post this question to mod_python's mailing list !-)

      Comment

      • Steve Holden

        #4
        Re: Relative paths in mod_python

        Ivo van der Sangen wrote:[color=blue]
        > I was wondering if I could use relative paths in a mod_python script. At
        > the moment I am defining a constant string
        > "/path/to/dir/where/script/resides". The problem with this is that when
        > I move the script including files I use to get metadata I have to change
        > this variable. The same problem occurs when I distribute the script; the
        > person using it will first have to modify it, which is not what I want. Is
        > it possible that a mod_python script has as current working directory
        > the directory where the script resides? At the moment os.getcwd()
        > returns '/'.[/color]

        import os
        req.write(os.pa th.abspath(__fi le__))

        seems to give you the absolute path to the module you are executing. You
        can use the other os.path primitives to extract the directory from that
        and use it to locate the associated data files.

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC/Ltd www.holdenweb.com
        Love me, love my blog holdenweb.blogs pot.com

        Comment

        • Ivo van der Sangen

          #5
          Re: Relative paths in mod_python

          On 2006-03-19, Bruno Desthuilliers <bdesth.quelque chose@free.quel quepart.fr>
          wrote:[color=blue]
          > Ivo van der Sangen a écrit :[color=green]
          >> I was wondering if I could use relative paths in a mod_python script. At
          >> the moment I am defining a constant string
          >> "/path/to/dir/where/script/resides". The problem with this is that when
          >> I move the script including files I use to get metadata I have to change
          >> this variable. The same problem occurs when I distribute the script; the
          >> person using it will first have to modify it, which is not what I want. Is
          >> it possible that a mod_python script has as current working directory
          >> the directory where the script resides? At the moment os.getcwd()
          >> returns '/'.[/color]
          >
          > You could set this constant in the apache conf as a PythonOption. And/Or
          > you could post this question to mod_python's mailing list !-)[/color]

          I wasn't aware of the mod_python list. The next time I will post my
          question there, but I don't think I will get a better answer there; the
          solutions given here so far are fine.

          Comment

          Working...