views:

29

answers:

4

Hi all-

I have a python module that is shared among several of my projects (the projects each have a different working directory). One of the functions in this shared module, executes a script using os.spawn. The problem is, I'm not sure what pathname to give to os.spawn since I don't know what the current working directory will be when the function is called. How can I reference the file in a way that any caller can find it? Thanks!

A: 

Put it in a well known directory (/usr/lib/yourproject/ or ~/lib or something similar), or have it in a well known relative path based on the location of your source files that are using it.

Burton Samograd
+1  A: 

So I just learned about the file variable, which will provide a solution to my problem. I can use file to get a pathname which will be constant among all projects, and use that to reference the script I need to call, since the script will always be in the same location relative to file. However, I'm open to other/better methods if anyone has them.

kkeogh
you mean '__file__', SO markup makes it bold.
Ivo van der Wijk
@kkeogh: You can change __file__ to `__file__` by putting backticks (`) around the string. There is also a button (it looks like "101\n010") in the editor to do the same thing.
unutbu
It's generally best to use `os.path.abspath(__file__)` to get the full path of the module, so that any later changes in CWD don't break it.
bobince
A: 

The following piece of code will find the location of the calling module, which makes sense from a programmer's point of view:

    ## some magic to allow paths relative to calling module
    if path.startswith('/'):
        self.path = path
    else:
        frame = sys._getframe(1)
        base = os.path.dirname(frame.f_globals['__file__'])
        self.path = os.path.join(base, path)

I.e. if your project lives in /home/foo/project, and you want to reference a script 'myscript' in scripts/, you can simply pass 'scripts/myscript'. The snippet will figure out the caller is in /home/foo/project and the entire path should be /home/foo/projects/scripts/myscript.

Alternatively, you can always require the programmer to specify a full path, and check using os.path.exists if it exists.

Ivo van der Wijk
A: 

You might find the materials in this PyCon 2010 presentation on cross platform application development and distribution useful. One of the problems they solve is finding data files consistently across platforms and for installed vs development checkouts of the code.

llasram