473,698 Members | 2,149 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(rScrip t)

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 1603
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(rScrip t)

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
17478
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 request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.IO.IOException: The directory name is invalid.
1
3277
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 Files\Test" it installs ok but will not start (no useful error message is given other than the usual annoying suggestion about having sufficient privileges). The problem only seems to happen with spaces, not long filenames. I have found a...
5
11045
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 created in the wrong folder on the hard drive. Before I discovered these files, .NET kept trying to create a new project with _1 following the project name. Deleting those files corrected that problem. I deleted the Virtual Directory, rebooted, and...
12
4033
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 pathname component and head is everything leading up to that. The tail part will never contain a slash; if path ends in a slash, tail will be empty. If there is no slash in path, head will be empty. If
13
1005
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 yields this message. Failed to map the path '/App_GlobalResources/' Stack trace: at System.Web.Hosting.HostingEnvironment.MapPathActual(VirtualPath virtualPath, Boolean permitNull)
5
8620
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". Same file path containing special characters works fine in one machine, but doesn't work in other. I am using following API function to get short file path. Declare Auto Function GetShortPathName Lib "kernel32" (ByVal lpszLongPath As
10
2944
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. Here's the script: # start of program # p.pl - fix broken SMTP headers in email files #
34
3957
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 environment variable. Every time, I've had to go through and add this by hand, to have something resembling a usable Python installation. No such problems on Linux, whether it be Mandrake/Mandriva, Fedora Core, or Kubuntu. So why is the Windows...
2
14129
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". Informational tokens are "DB2 v8.1.1.104", "s060120", "U805924", and
5
10038
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 this is because using a UNC paths makes the path longer, causing the periodic problem with a path that is too long (over 260 chars). We also have an asp.net app that needs to access those files. Accessing mapped drives from an IIS application is...
0
8674
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9028
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7728
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4369
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.