Re: Inline::Python and PYTHONPATH --SOLVED
[email protected] (ejm) Wed, 25 Jul 2018 07:04:53 -0700 (GMT-07:00)
| Newsgroups | perl.inline |
|---|---|
| Message-ID | <[email protected]> |
<html><head><style type=3D"text/css"><!-- DIV {margin:0px;} --></style></he=
ad><body><div style=3D"font-size: 13px;color: rgb(0, 0, 0);font-family: ari=
al,sans-serif;"><style type=3D"text/css"><!-- DIV {margin:0px;} --></style>=
<div style=3D"font-size: 13px;color: rgb(0, 0, 0);font-family: arial,sans-s=
erif;">Hi all,<br><br>Many thanks for the suggestions to put the declaratio=
n 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 Pyt=
hon code to execute.<br><br>I then found two other issues to solve, the fir=
st involving the Perl namespace of my codebase, and the second a small requ=
irement of the Python module httpie.<br><br>I'll detail these 2 issues in c=
ase others also encounter them.<br><br>1) If the Python code is embedded an=
d called from within a Perl package in a namespace one may get the error me=
ssage 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 appea=
rs 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 ha=
sh given by the API key 'pkg'.<br><br>As an example, my original code snipp=
et is:<br><br> <font face=3D"courier new,monospace">package X::Y::Z::Core;<=
br><br>... other use statements and comments...<br><br>use Inline::Files;<b=
r>use Inline (Python =3D> 'file', directory =3D> '/tmp/_Inline');<br>=
use Inline::Python; <br><br>... lots of other Perl statements<br><br> =
<b>my $pyobj =3D new VSO_WS;</b><br> $pyobj->{'dbquery'} =3D $block;<=
br> $query =3D $pyobj->get_data();<br><br>... more processing stateme=
nts<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 d=
eclare PYTHONPATH before Inline is initialized, the error message I got whe=
n executing the above code snippet was that the Python method <font face=3D=
"courier new,monospace">get_data</font> was 'not found'.<br><br>The solutio=
n was to fully qualify the Perl namespace path to VSO_WS so that the addres=
s used in the Inline code would work correctly when called from Perl.<br><b=
r>Changing the line in <b>BOLD</b> above to:<br><br><font face=3D"courier n=
ew,monospace">my $pyobj =3D new X::Y::Z::Core::VSO_WS;</font><br><br>worked=
!<br><br>No error messages about <font face=3D"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 Apac=
he, will need a directory <font face=3D"courier new,monospace">.httpie</fon=
t> to be created initially beforehand. Once directory<font face=3D"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=3D"courier new,monospace">.httpi=
e</font> should be owned by Apache.<br><br><br>Thanks again for all the ver=
y helpful replies,<br><br>--Ed<br><br></div></div></body></html>