473,396 Members | 1,875 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 18 '05 #1
4 1591
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Glenn A. Harlan | last post by:
Why am I receiving the below error when calling - Path.GetTempFileName() The directory name is invalid. Description: An unhandled exception occurred during the execution of the current web...
1
by: bob | last post by:
I have created a simple Windows service in VB.Net which installs fine using InstallUtil.exe to install it to, for example "c:\test", or "c:\Windows\YellowBanana", but if I install it to "c:\Program...
5
by: David Webb | last post by:
The problem started when the Working Folder for a project was somehow set to the folder of another project. I set the correct working folder in VSS and deleted the .vbproj files that had been...
12
by: Xah Lee | last post by:
Python Doc Problem Example Quote from: http://docs.python.org/lib/module-os.path.html ---------- split( path) Split the pathname path into a pair, (head, tail) where tail is the last...
13
by: Adam | last post by:
Trying to get an asp.net 2.0 app running and am receiving this error. I see a bunch of people with this error on the net, but no solution: Works fine on my local machine, deployed to a server it...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
10
by: wo_shi_big_stomach | last post by:
Newbie to python writing a script to recurse a directory tree and delete the first line of a file if it contains a given string. I get the same error on a Mac running OS X 10.4.8 and FreeBSD 6.1. ...
34
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
2
by: contractsup | last post by:
Environment: $ uname -a AIX <withheld2 5 000100614C00 $ db2level DB21085I Instance "<withheld>" uses "32" bits and DB2 code release "SQL08024" with level identifier "03050106"....
5
by: =?Utf-8?B?SmVmZiBCZWVt?= | last post by:
Before you respond with "just use GetShortPathName" please read the rest. We have an application that places files on a file server using mapped drives as it's path of choice. The reason for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.