Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Path problem

Reply
Thread Tools

Path problem

 
 
Lars Yencken
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
 
 
 
Binu K S
Guest
Posts: n/a
 
      12-13-2004
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 <(E-Mail Removed)> 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
>

 
Reply With Quote
 
 
 
 
Lars Yencken
Guest
Posts: n/a
 
      12-13-2004
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

 
Reply With Quote
 
Binu K S
Guest
Posts: n/a
 
      12-13-2004
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 <(E-Mail Removed)> 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
>
>

 
Reply With Quote
 
Lars Yencken
Guest
Posts: n/a
 
      12-14-2004
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Server.MapPath() croaking with "failed to map path", but path is legal awbacker@gmail.com ASP .Net 0 05-12-2005 10:50 PM
embedded perl path $ENV{PATH} problem R. Sherwin Kartick Perl Misc 0 09-22-2004 10:29 PM
Bizzare: Invalid path to Access mdb, path is good Keith-Earl ASP .Net 1 05-03-2004 03:25 PM
problem in appending a path to sys.path ketulp_baroda@yahoo.com Python 3 02-26-2004 06:03 AM
Tomcat returning 404 error when using additional path (/servlet/myServlet/extra/path/info) Mupota Muchelemba Java 1 02-04-2004 12:43 AM



Advertisments