Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Path problem (http://www.velocityreviews.com/forums/t339152-path-problem.html)

Lars Yencken 12-13-2004 04:48 AM

Path problem
 
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


Binu K S 12-13-2004 05:11 AM

Re: Path problem
 
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 <lljy@internode.on.net> 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
>


Lars Yencken 12-13-2004 07:03 AM

Re: Path problem
 
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 12-13-2004 07:39 AM

Re: Path problem
 
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 <lljy@internode.on.net> 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
>
>


Lars Yencken 12-14-2004 12:09 AM

Re: Path problem
 
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



All times are GMT. The time now is 08:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.