473,387 Members | 1,790 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,387 software developers and data experts.

paths in modules

I am developing a project in Python which uses several external utilities.
For convenience, I am wrapping these accesses in a module. The problem is
that I cannot be sure where these modules are imported from, so I am
trying to figure out how to reliably execute e.g. a popen call. Example
layout:

toplevel_dir
+-main script
+-wrapper_dir
+-some_wrapper
+-utility_dir
+-some_external_utility

So in my main script, I might say:

from wrapper_dir import some_wrapper

some_wrapper.use_external_utility()
And then in some_wrapper, I would have code like:

import os

def use_external_utility():
f = os.popen('utility_dir/some_external_utility')
lines = f.readlines()
f.close()
return lines
Of course, the problem with that approach is that it fails because there
is no utility_dir in the CWD, which is actually top_level_dir. So my
question is whether there is any way to specify that specified paths are
relative to the module's directory rather than the importing file's
directory. I would really like to avoid kludging together some solution
that involves passing variables or having knowledge of where my module is
being imported from.

I am hoping that there is some simple solution to this problem that I
simply haven't found in my searches so far. If so, I will humbly accept
any ridicule that comes along with said simple solution :-).

Thanks in advance,
Brandon
Feb 22 '07 #1
5 1001
On Thu, 22 Feb 2007 11:13:46 -0500, Brandon Mintern wrote:
Of course, the problem with that approach is that it fails because there
is no utility_dir in the CWD...
....and of course by CWD, I actually mean "current working directory",
which should have actually been PWD or "present working directory".
Anyway, I just wanted to clear up any confusion that might result due to
my improper terminology.
Feb 22 '07 #2
On 22 Feb, 17:13, Brandon Mintern <mint...@cse.ohio-state.eduwrote:
>
toplevel_dir
+-main script
+-wrapper_dir
+-some_wrapper
+-utility_dir
+-some_external_utility
[...]
And then in some_wrapper, I would have code like:

import os

def use_external_utility():
f = os.popen('utility_dir/some_external_utility')
lines = f.readlines()
f.close()
return lines
And you really want to refer to utility_dir relative to some_wrapper.
What you can try is to split the __file__ attribute of some_wrapper -
it's a standard attribute on imported modules - in order to refer to
the module's parent directory (which should correspond to
wrapper_dir):

parent_dir, filename = os.path.split(__file__)

Then you can join the parent directory to the path of the command:

cmd = os.path.join(parent_dir, "utility_dir", "some_external_utility")

The __file__ attribute of modules is documented here:

http://docs.python.org/ref/types.html#l2h-109

Paul

Feb 22 '07 #3
Brandon Mintern wrote:
I am developing a project in Python which uses several external utilities.
For convenience, I am wrapping these accesses in a module. The problem is
that I cannot be sure where these modules are imported from, so I am
trying to figure out how to reliably execute e.g. a popen call. Example
layout:

toplevel_dir
+-main script
+-wrapper_dir
+-some_wrapper
+-utility_dir
+-some_external_utility

So in my main script, I might say:

from wrapper_dir import some_wrapper

some_wrapper.use_external_utility()
And then in some_wrapper, I would have code like:

import os

def use_external_utility():
f = os.popen('utility_dir/some_external_utility')
lines = f.readlines()
f.close()
return lines
Of course, the problem with that approach is that it fails because there
is no utility_dir in the CWD, which is actually top_level_dir. So my
question is whether there is any way to specify that specified paths are
relative to the module's directory rather than the importing file's
directory. I would really like to avoid kludging together some solution
that involves passing variables or having knowledge of where my module is
being imported from.

I am hoping that there is some simple solution to this problem that I
simply haven't found in my searches so far. If so, I will humbly accept
any ridicule that comes along with said simple solution :-).

Thanks in advance,
Brandon
Normally this would be:

f = os.popen('./wrapper_dir/utility_dir/some_external_utility')

-Larry
Feb 22 '07 #4
On Thu, 22 Feb 2007 08:28:50 -0800, Paul Boddie wrote:
And you really want to refer to utility_dir relative to some_wrapper.
What you can try is to split the __file__ attribute of some_wrapper -
it's a standard attribute on imported modules - in order to refer to
the module's parent directory (which should correspond to
wrapper_dir):

parent_dir, filename = os.path.split(__file__)

Then you can join the parent directory to the path of the command:

cmd = os.path.join(parent_dir, "utility_dir", "some_external_utility")

The __file__ attribute of modules is documented here:

http://docs.python.org/ref/ty__file__ is the pathname of the file from
which the module was loadedpes.html#l2h-109

Paul
Thanks a lot. I was hoping for a solution like that, and it worked
perfectly for me (admittedly, in my trivial test files, but I'm sure that
the solution will extend to my actual use case).

Also, I had actually read that documentation you pointed me to, but the
language, "__file__ is the pathname of the file from which the module was
loaded" had me thinking that it was the pathname of the file doing the
loading, rather than the file being loaded. I guess I should have
actually tried it out.

Anyways, thanks for the quick response and for the clarification.
Feb 22 '07 #5
On Thu, 22 Feb 2007 10:30:59 -0600, Larry Bates wrote:
Normally this would be:

f = os.popen('./wrapper_dir/utility_dir/some_external_utility')

-Larry
Yes, but the problem with that solution is, let's say that I further
abstract the whole thing and I add a directory outside of my toplevel_dir,
which has the syntax:

from toplevel_dir.wrapper_dir import some_wrapper

some_wrapper.use_external_utility()
Now, once again, the module is broken. This is what I was trying to
avoid. At any rate, Paul's solution was exactly what I was looking for.
Feb 22 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: peter walker | last post by:
I downloaded a code example for Planet Soiurce Code and when I try to open the project file I keep getting path errors. In some languages you can go in and change the project paths, is there a way...
5
by: Noah | last post by:
Does anyone have a function to convert back and forth between NT style paths and POSIX style? It seems trivial, but I want to make sure I don't overlook some obscure detail. Is it a simple matter...
0
by: sashan | last post by:
Hi How do you import a module in a path that's not in sys.path. I've got a project and would like to split the .py files into several differrent directories, say dir_a, dir_b, dir_c. Then I...
2
by: Raaijmakers, Vincent \(GE Infrastructure\) | last post by:
In my enthusiasm of separating my unittests from my business logic modules, I encounter some problems in my path setting. Can solve them, but so ugly. Needs suggestions in the right way to do it. ...
9
by: Amir Dekel | last post by:
Hi everyone, I have two problems: 1. How can I keep my changes in sys.path after closing the interpreter? 2. os.path.expanduser("~") always gives me "C:\\" instead of my homepath. I have...
5
by: Bill Davis | last post by:
I am just starting to play with CGI programs on a box with RH 9. Testing to see if I had the stuff installed I used a very simple script like so... #!/usr/bin/perl -w use strict; use CGI; ...
24
by: Lovely Angel | last post by:
Dear Friends Hope you doing great. I have recently shifted to a webhost which is Using Windows 2003 and IIS 6. Now my application was working fine earlier but now I am facing this problem ...
6
by: David Siroky | last post by:
Hi! When I "compile" my python files with "python -OO ...." into pyo files then they still contain absolute paths of the source files which is undesirable for me. How can I deal with that? ...
5
by: David Mathog | last post by:
One thing that can make porting C code from one platform to another miserable is #include. In particular, the need to either place the path to an included file within the #include statement or to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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,...

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.