Getting current working directory (CWD) using Python on Mac

I’m trying to import modules next to the application I’m building but when using os.path.getcurdir() it returns “/” instead of the actual path. Which would be fine… except Mac can’t use that for finding the modules in the same folder The only way I’ve found to get around it is to hardcode the path, and thats dangerous and a hacky way to do things. This doesn’t make any sense to me because when typing it into the console it returns “/Users/test” which is correct. I’ve tried various other commands like os.path.abspath(os.path.curdir) but they all return the same thing.

Does anyone know how to get the current working directory on a Mac using Python2.5?


os.path.curdir is failing for me as well when run inside blender (it returns ‘.’ on windows xp), no idea why. On windows I can use this though:

import Blender
print Blender.sys.dirname(Blender.Get('filename'))

No idea if it works on the Mac, but it might be worth a try.
Note: this will only work if you run it in Blender when the file is actually saved somewhere, it won’t work in a new default scene that hasn’t been saved yet. So first save, then run the script.

Thanks Crouch!

That method works perfectly when running it from Blender, but when I turn it into an application it fails on importing Blender. Arrrrghhh… the Windows one imports Blender fine when it’s a runtime. I always assumed that the Blender module was something that was included in the application/runtime when compiling, is it actually a seperate file somewhere else? If it is then I guess I could place it into the /Library using an installer, hardcode the path to it and then use it to find the CWD for the rest of the files. It would mean they couldn’t just drag’n’drop the app from a CD but at least they’d be able to move it around after installing.

Sorry, I missed that you were trying to code this for an external application.
I think you’ll want this:

import sys
print sys.path[0]

Try to read this :

On the Mac that returns the path to the Blender application when running the project file, and the path to the Python framework when running the compiled application.

An interesting read for sure, but that doesn’t help me with importing Blender to use any of that in the application :frowning:

Trying to bundle anything in a Mac is a nightmare. We’re buying another Mac here to test if we’re packing (and linking to) all the right files. So far the most acceptable solution seems to be making an installer that places pygames, our python scripts, sounds, and the database inside the Library/Application Support/{company name}/{product name}/ and adding os.path.apppend() to link to them.

Another issue that I’m trying to deal with is accessing network shares using python. With windows its simple, just put in //{computername}/{share}/ and you’re off laughing. With the mac you have to mount the share first, and it doesn’t tell you in the ‘Get info’ what the path to it is (at least, not in a way that my python program can use it). To make things more complicated, the path changes depending on the way you mount it. Using the Go>Network and navigating to the server produces the path “/Volumes/WORKGROUP;{servername}/” which worries me because that doesn’t include the name of the share I just accessed. Alternatively if I setup a Login Item then the path becomes “/Volumes/{sharename}/”. Does anyone have any experience with accessing network shares on Macs using python or could point me to some information on the best way to do it? I’ve googled it a fair bit but searching for anything with “mac” in it tends to turn up information on MAC addresses, not .Mac addresses. Purhaps I’m using the wrong search terms.