Path problem

Discussion in 'Python' started by Lars Yencken, Dec 13, 2004.

  1. Lars Yencken

    Lars Yencken Guest

    Hello,

    I'm working on a project where my python modules are using persistent
    files in the same directory. As an example, we're using rpy, so a piece
    of python code might read:

    from rpy import *

    rScript = 'myScript.r'

    r.source(rScript)

    Now the problem with this is that when I run this from the directory
    above, or any other directory than the directory containing the R
    script, this doesn't work, and understandably too.

    Is there some easy way to get around this? I can't (and don't want to)
    hard code paths, since they'll be different on every workspace I check
    out from the repository.

    If there's a way that I can get the directory of the module file
    itself, then that's all I need to fix this for good.

    Thanks,
    Lars
     
    Lars Yencken, Dec 13, 2004
    #1
    1. Advertising

  2. Lars Yencken

    Binu K S Guest

    This should get you the module's path:

    import sys
    sys.modules['rpy'].__file__

    On Mon, 13 Dec 2004 15:48:29 +1100, Lars Yencken <> wrote:
    > Hello,
    >
    > I'm working on a project where my python modules are using persistent
    > files in the same directory. As an example, we're using rpy, so a piece
    > of python code might read:
    >
    > from rpy import *
    >
    > rScript = 'myScript.r'
    >
    > r.source(rScript)
    >
    > Now the problem with this is that when I run this from the directory
    > above, or any other directory than the directory containing the R
    > script, this doesn't work, and understandably too.
    >
    > Is there some easy way to get around this? I can't (and don't want to)
    > hard code paths, since they'll be different on every workspace I check
    > out from the repository.
    >
    > If there's a way that I can get the directory of the module file
    > itself, then that's all I need to fix this for good.
    >
    > Thanks,
    > Lars
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Binu K S, Dec 13, 2004
    #2
    1. Advertising

  3. Lars Yencken

    Lars Yencken Guest

    Hi Binu,

    On 13/12/2004, at 4:11 PM, Binu K S wrote:
    > This should get you the module's path:
    >
    > import sys
    > sys.modules['rpy'].__file__
    >


    Unfortunately it's not the rpy module itself whose path I'm looking
    for. It's the absolute path of my module that I've created.

    If my script was called runRScript.py, and it was in the same directory
    as someScript.r, I'm only able to get it working if I run

    python runRScript.py

    from the same directory. In otherwords, I can't be in a child directory
    and run:

    python ../runRScript.py

    because runRScript depends internally on the file someScript.r, and
    can't find it.

    I've found a pathconf module posted earlier to this group, which does
    half the job. Unfortunately, when I call get_rootdir() it returns
    '/usr/bin' instead of my project's root directory ;(

    Lars
     
    Lars Yencken, Dec 13, 2004
    #3
  4. Lars Yencken

    Binu K S Guest

    Hi Lars,

    sys.path[0] will contain the path to the script.

    >From the sys module documentation:

    "As initialized upon program startup, the first item of this list,
    path[0], is the directory containing the script that was used to
    invoke the Python interpreter. If the script directory is not
    available (e.g. if the interpreter is invoked interactively or if the
    script is read from standard input), path[0] is the empty string,
    which directs Python to search modules in the current directory first.
    Notice that the script directory is inserted before the entries
    inserted as a result of PYTHONPATH."

    -Binu

    On Mon, 13 Dec 2004 18:03:30 +1100, Lars Yencken <> wrote:
    > Hi Binu,
    >
    > On 13/12/2004, at 4:11 PM, Binu K S wrote:
    > > This should get you the module's path:
    > >
    > > import sys
    > > sys.modules['rpy'].__file__
    > >

    >
    > Unfortunately it's not the rpy module itself whose path I'm looking
    > for. It's the absolute path of my module that I've created.
    >
    > If my script was called runRScript.py, and it was in the same directory
    > as someScript.r, I'm only able to get it working if I run
    >
    > python runRScript.py
    >
    > from the same directory. In otherwords, I can't be in a child directory
    > and run:
    >
    > python ../runRScript.py
    >
    > because runRScript depends internally on the file someScript.r, and
    > can't find it.
    >
    > I've found a pathconf module posted earlier to this group, which does
    > half the job. Unfortunately, when I call get_rootdir() it returns
    > '/usr/bin' instead of my project's root directory ;(
    >
    > Lars
    >
    >
     
    Binu K S, Dec 13, 2004
    #4
  5. Lars Yencken

    Lars Yencken Guest

    On 13/12/2004, at 6:39 PM, Binu K S wrote:
    > sys.path[0] will contain the path to the script.
    >
    > From the sys module documentation:
    > "As initialized upon program startup, the first item of this list,
    > path[0], is the directory containing the script that was used to
    > invoke the Python interpreter. If the script directory is not
    > available (e.g. if the interpreter is invoked interactively or if the
    > script is read from standard input), path[0] is the empty string,
    > which directs Python to search modules in the current directory first.
    > Notice that the script directory is inserted before the entries
    > inserted as a result of PYTHONPATH."


    Thanks, that's very helpful!!! That's fixed up a lot of scripts of mine
    already, since many rely on an additional R script.

    As to finding the path to my project root, I've managed to get the
    other pathconf module going. Yay!

    Thanks Binu and Limodou for your help.

    Lars
     
    Lars Yencken, Dec 14, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Keith-Earl
    Replies:
    1
    Views:
    2,012
    Peter O'Reilly
    May 3, 2004
  2. Replies:
    0
    Views:
    2,281
  3. Mupota Muchelemba
    Replies:
    1
    Views:
    944
    Tony Morris
    Feb 4, 2004
  4. Replies:
    3
    Views:
    409
  5. R. Sherwin Kartick

    embedded perl path $ENV{PATH} problem

    R. Sherwin Kartick, Sep 22, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    195
    R. Sherwin Kartick
    Sep 22, 2004
Loading...

Share This Page