paths in modules
Larry Bates
lbates at websafe.com
Thu Feb 22 11:30:59 EST 2007
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
More information about the Python-list
mailing list