develooper Front page | perl.inline | Postings from July 2018

Re: Inline::Python and PYTHONPATH --SOLVED

July 25, 2018 14:05
Re: Inline::Python and PYTHONPATH --SOLVED
Message ID:
<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-size: 13px;color: rgb(0, 0, 0);font-family: arial,sans-serif;"><style type="text/css"><!-- DIV {margin:0px;} --></style><div style="font-size: 13px;color: rgb(0, 0, 0);font-family: arial,sans-serif;">Hi all,<br><br>Many thanks for the suggestions to put the declaration of the environment variable  PYTHONPATH in a BEGIN block before Inline is called.<br><br>That did the trick of getting the Perl code to find the Python code to execute.<br><br>I then found two other issues to solve, the first involving the Perl namespace of my codebase, and the second a small requirement of the Python module httpie.<br><br>I'll detail these 2 issues in case others also encounter them.<br><br>1) If the Python code is embedded and called from within a Perl package in a namespace one may get the error message when running the Perl code that the Python method is 'not found' even though the Python module itself was found and entered.<br><br><br>It appears that the Inline modules sets up the Python class that is imported,  as a Perl package to be part of the Perl namespace located at the Inline API hash given by the API key 'pkg'.<br><br>As an example, my original code snippet is:<br><br> <font face="courier new,monospace">package X::Y::Z::Core;<br><br>... other use statements and comments...<br><br>use Inline::Files;<br>use Inline (Python =&gt; 'file', directory =&gt; '/tmp/_Inline');<br>use Inline::Python;   <br><br>... lots of other Perl statements<br><br>    <b>my $pyobj = new VSO_WS;</b><br>    $pyobj-&gt;{'dbquery'} = $block;<br>    $query = $pyobj-&gt;get_data();<br><br>... more processing statements<br><br>1;<br><br>____PYTHON__<br><br>from VSO import VSO_WS</font><br><br>The key line is in <b>BOLD</b>.<br><br>After adding the BEGIN block to declare PYTHONPATH before Inline is initialized, the error message I got when executing the above code snippet was that the Python method <font face="courier new,monospace">get_data</font> was 'not found'.<br><br>The solution was to fully qualify the Perl namespace path to VSO_WS so that the address used in the Inline code would work correctly when called from Perl.<br><br>Changing the line in <b>BOLD</b> above to:<br><br><font face="courier new,monospace">my $pyobj = new X::Y::Z::Core::VSO_WS;</font><br><br>worked!<br><br>No error messages about&nbsp; <font face="courier new,monospace">get_data</font>, and data is indeed returned from Python to Perl as needed.<br><br>2) The Python module httpie, when executed from a codebase as Apache, will need a directory <font face="courier new,monospace">.httpie</font> to be created initially beforehand. Once directory<font face="courier new,monospace"> .httpie</font> is created, the python code using http to do a GET operation on a remote Python web service will then be executed. NOTE: The initially empty directory <font face="courier new,monospace">.httpie</font> should be owned by Apache.<br><br><br>Thanks again for all the very helpful replies,<br><br>--Ed<br><br></div></div></body></html> Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About