kkeogh
kkeogh

Reputation: 47

Determining a file's path name from different working directories in python

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!

Upvotes: 0

Views: 183

Answers (4)

kkeogh
kkeogh

Reputation: 47

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.

Upvotes: 1

llasram
llasram

Reputation: 4485

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.

Upvotes: 0

Ivo van der Wijk
Ivo van der Wijk

Reputation: 16795

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.

Upvotes: 0

Burton Samograd
Burton Samograd

Reputation: 3638

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.

Upvotes: 0

Related Questions