By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,461 Members | 1,001 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,461 IT Pros & Developers. It's quick & easy.

Path problem

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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 <ll**@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

Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
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 <ll**@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

Jul 18 '05 #4

P: n/a
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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.