Python can open file, PL/Python can't
<p>Connected to mydb in PostgreSQL:</p>

<pre><code>mydb=# CREATE FUNCTION file_test () RETURNS text AS $$
if open('mydir/myfile.xsl'): return 'success' 
$$ LANGUAGE plpythonu;
CREATE FUNCTION
mydb=# SELECT file_test();
ERROR:  IOError: [Errno 2] No such file or directory: 'mydir/myfile.xsl'
CONTEXT:  Traceback (most recent call last):
  PL/Python function "file_test", line 2, in &lt;module&gt;
    if open('mydir/myfile.xsl'): return 'success'
PL/Python function "file_test"
</code></pre>

<p>Over to Python:</p>

<pre><code>&gt;&gt;&gt; if open('mydir/myfile.xsl'): print 'success'
... 
success
&gt;&gt;&gt; 
</code></pre>

<p>An absolute path didn't seem to help PL/Python. What I'd like to do is use Postgres's query_to_xml() and run an XSLT transformation on the return. But to do that I'll need to read the xsl file...</p>

<p>At @hruske's suggestion I used <code>plpy.notice(os.path.abspath('mydir/myfile.xsl'))</code> to see how PL/Python was trying to resolve the path. It was <code>/var/lib/postgresql/9.5/main/mydir/myfile.xsl</code>, which was obviously not what I had in mind.</p>

<p>The absolute path worked after all. Copying the file to a location that was easier to type as an absolute path fixed the "problem." </p>

<p>For further reading I recommend Soko Morinaga's <a href="http://rads.stackoverflow.com/amzn/click/0861713931" rel="nofollow">Novice to Master: An Ongoing Lesson in the Extent of My Own Stupidity</a>.</p>

python postgresql xslt plpython