You are not logged in.

#1 2007-04-30 09:40:14

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

[Python] Relative paths in modules

Hi, my Python texts and Google are simply not forthcoming with my current problem.

Say I have created a python module called /python_modules/MyModule.py. In the same dir is another sub-dir called 'xml' thus /python_modules/xml/ and in there are lots of xml files. These xml files are ready by MyModule.py when it's initialised.

So, you can imagine in MyModule.py there are calls like 'loadXmlFile('xml/AnXmlFile.xml'), which as you can see, are relative calls. This is handy because I don't want to hard-code the absolute into the module.

The issue is that if I have a bit of code in /home/andyr/MyPythonCode.py which calls MyModule.py, the latter assumes the external files are located relative to the current working directory, i.e., /home/andyr/xml/AnXmlFile.xml rather than /python_modules/xml/AnXmlFile.xml.

Hope this makes sense so far. So clearly my question is how do I tell MyModule to work relative to itself rather than the caller's path?

TIA

Offline

#2 2007-04-30 10:43:58

sl
Banned
From: uk
Registered: 2007-04-04
Posts: 42
Website

Re: [Python] Relative paths in modules

i hope i understood ur post ...
as i read it,

when another app is called it inherits it's caller's cwd as expected, which causes issues ,,
the best and only solution i can think of is to just hardcode the paths in MyModule to it's intended wd ..
`os.path.dirname(sys.argv[0])`

------------------

i just tested it with

z.py)
#!/usr/bin/python
import os, sys
print 'z:cwd:', os.getcwd()
print 'z:resides:', os.path.dirname(sys.argv[0])
os.system('/tmp/py/zz.py')


######

zz.py)
#!/usr/bin/python
import os, sys
print 'zz:cwd:', os.getcwd()
print 'zz:resides:', os.path.dirname(sys.argv[0])


######

##output

[noriko@Lux64  ~]# /tmp/z.py
z:cwd: /noriko
z:resides: /tmp
zz:cwd: /noriko
zz:resides: /tmp/py

Offline

#3 2007-04-30 10:58:14

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: [Python] Relative paths in modules

i can't think of any 'nice' way of doing that, but i always found with python, that if there isnt a nice way, there's probably a better way of dealing with the problem.

i'd hardcode it, as if it's something you want to distribute, im sure you can use setup tools to create a setup.py that will fix the paths for the application on install on another system.

James

Offline

#4 2007-04-30 16:13:19

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: [Python] Relative paths in modules

How are you calling MyModule.py from MyPythonCode.py? What I mean is, essentially, how does the latter know where the former is?

Offline

#5 2007-04-30 17:02:26

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: [Python] Relative paths in modules

gradgrind wrote:

How are you calling MyModule.py from MyPythonCode.py? What I mean is, essentially, how does the latter know where the former is?

Because I've set the PYTHONPATH environment variable to point to the directory containing my modules. Python looks up this env and adds it to the module search path.

Offline

#6 2007-04-30 17:28:45

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: [Python] Relative paths in modules

So in a way it is a sort of hard-coded path. The question would then be, what sort of flexibility do you need in finding this path? An obvious, and rather inelegant, way would be to search for the module or directory in the module search path.

Offline

#7 2007-04-30 18:11:14

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: [Python] Relative paths in modules

gradgrind wrote:

So in a way it is a sort of hard-coded path. The question would then be, what sort of flexibility do you need in finding this path? An obvious, and rather inelegant, way would be to search for the module or directory in the module search path.

I don't really agree that because an environment variable is set that the path has already been hard-coded - because it's not in the source code!

But I agree that it looks like I will have to fall back with an inelegant solution and yours to search the module path isn't that bad, I suppose. Ta.

Offline

#8 2007-04-30 18:38:20

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: [Python] Relative paths in modules

Well, one workaround I've found is to use sys to lookup the currently loaded module list.

import sys
print sys.modules['MyModule'].__file__

And that will print the full path of the .py file. I can process that to get the parent dir and use that therein.

Offline

#9 2007-04-30 22:18:01

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: [Python] Relative paths in modules

MyModule.__file__

should work too, roo. Assuming its already loaded.

Offline

#10 2007-05-01 05:21:37

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: [Python] Relative paths in modules

Well done, lads! It had occurred to me that python must keep a record of module locations because it lists the paths in error reports, but finding that in the documentation is apparently not so easy (unless I am blind...).
There is just one little point I discovered - it may return the file with a different ending (.pyc) because of compilation. But that should be no problem if you only want the directory.

Offline

#11 2007-05-01 07:28:22

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: [Python] Relative paths in modules

T-Dawg wrote:
MyModule.__file__

should work too, roo. Assuming its already loaded.

Already tried that and it doesn't seem to work if called by the module itself. That's the tricky think here, the module needs to know where it is, not the code that's calling it.

Using sys works fine.

Offline

#12 2007-05-01 08:16:28

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: [Python] Relative paths in modules

arooaroo wrote:
T-Dawg wrote:
MyModule.__file__

should work too, roo. Assuming its already loaded.

Already tried that and it doesn't seem to work if called by the module itself. That's the tricky think here, the module needs to know where it is, not the code that's calling it.

Using sys works fine.

Use the module attribute __file__ (i.e. without the MyModule prefix).

Offline

#13 2007-05-01 12:50:55

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: [Python] Relative paths in modules

gradgrind wrote:

Use the module attribute __file__ (i.e. without the MyModule prefix).

I couldn't seem to call it. My module in question contains a class. In the __init__ of the class is where I need the path info. Calling self.__file__ didn't work, but that was kinda obvious since it's not a class attribute. How else do I get to this module attribute?

Thanks

Offline

#14 2007-05-01 13:03:01

gradgrind
Member
From: Germany
Registered: 2005-10-06
Posts: 921

Re: [Python] Relative paths in modules

Just __file__ (as in 'print __file__') - nothing else. It worked in my test.

Offline

Board footer

Powered by FluxBB