Using python in D

Russel Winder russel at winder.org.uk
Sat Jun 8 09:11:48 UTC 2019


On Fri, 2019-06-07 at 14:02 +0000, rnd via Digitalmars-d-learn wrote:

This being D meets Python, I thought I'd take a look at this one.

> I tried following in dub.selections.json file:
> 
> {
> 	"fileVersion": 1,
> 	"versions": {
> 		"pyd": "0.10.5"
> 	},
> 	"subConfigurations": {
> 		"pyd": "python35"
> 	}
> }

I do not use JSON for human constructed files, and whilst I think SDL
is inferior to TOML for these sorts of files, I am using the Dub
specification:

   name "pyd_test"
   description "A PyD project from rnd on the email list."
   dependency "pyd" version="*"
   subConfiguration "pyd" "python37"

which seems to do the job.

> I also tried python3.5 and python3 in place of python35, but 
> everytime python27 is being used (where pandas is not installed). 
> The error is:

I only have Python 3.7, I deleted Python 2.7 long ago, and always use
the latest Python on Debian (which may get a bit old during the stupid
process called Debian release freeze.

> $ dub run
> Performing "debug" build using /usr/bin/dmd for x86_64.
> pyd 0.10.5: target for configuration "python27" is up to date.   
> <<< NOTE VERSON HERE;
> rntestpy ~master: building configuration "application"...
> Linking...
> To force a rebuild of up-to-date targets, run again with --force.
> Running ./rntestpy
> pyd.exception.PythonException@/home/cardio/.dub/packages/pyd-
> 0.10.5/pyd/infrastructure/pyd/pydobject.d(59):
> exceptions.ImportError: No module named pandas
> ----------------
> /home/cardio/.dub/packages/pyd-
> 0.10.5/pyd/infrastructure/pyd/exception.d:46 void
> pyd.exception.handle_exception(immutable(char)[], ulong)
> [0x56438721e9e8]
> /home/cardio/.dub/packages/pyd-
> 0.10.5/pyd/infrastructure/pyd/pydobject.d:59 pyd.pydobject.PydObject
> pyd.pydobject.PydObject.__ctor(deimos.python.object.PyObject*)
> [0x56438721f2e8]
> /home/cardio/.dub/packages/pyd-
> 0.10.5/pyd/infrastructure/pyd/embedded.d:54 pyd.pydobject.PydObject
> pyd.embedded.py_import(immutable(char)[]) [0x56438721e501]
> source/main.d:6 _Dmain [0x56438721ded0]
> Program exited with code 1
> 
> I also tried command "dub run --force" but still the error 
> persists.

I am getting what appears to be the exact same problem:

   |> dub
   Performing "debug" build using /usr/bin/ldc2 for x86_64.
   pyd 0.10.5: target for configuration "python37" is up to date.
   pyd_test ~master: building configuration "application"...
   To force a rebuild of up-to-date targets, run again with --force.
   Running ./pyd_test 
   pyd.exception.PythonException at source/main.d(10): 
   NameError: name 'pandas' is not defined
   ----------------
   exception.d:46 [0x564147a72be6]
   embedded.d:283 [0x564147a72a23]
   main.d:10 [0x564147a6fce1]
   ??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x7f8116bff12f]
   ??:? _d_run_main [0x7f8116bfef45]
   __entrypoint.d:8 [0x564147a70314]
   ??:? __libc_start_main [0x7f81167d909a]
   ??:? [0x564147a6fb99]
   Program exited with code 1

But I am quietly confident that pandas is installed into the standard
place:

|> aptitude search python3-pandas
i   python3-pandas                                                                                 - data structures for "relational" or "labeled" data - Python 3                                           
i A python3-pandas-lib                                                                             - low-level implementations and bindings for pandas - Python 3                                            

So, a priori, this would seem to be a fault within PyD for not looking
up the right place for Python packages.

Having looked at the dub.json ( :-( ) file for PyD it seems that it is
looking in /usr/lib/python3.7m whereas on Debian the directory should
be /usr/lib/python3.7. I have no idea how to control this from a user
dub.sdl.

Of course the installed Python packages are in /usr/lib/python3/dist-
packages, at least on Debian, so the above shouldn't apply!

> I also tried to give command "source pyd_set_env_vars.sh 
> /usr/bin/python3" but still it is not working.
> 
> Another command which I tried produced following output but still 
> the problem persisted:
> 
> $ python3 pyd_get_env_set_text.py
> export PYD_D_VERSION_1=Python_2_4_Or_Later
> export PYD_D_VERSION_2=Python_2_5_Or_Later
> export PYD_D_VERSION_3=Python_2_6_Or_Later
> export PYD_D_VERSION_4=Python_2_7_Or_Later
> export PYD_D_VERSION_5=Python_3_0_Or_Later
> export PYD_D_VERSION_6=Python_3_1_Or_Later
> export PYD_D_VERSION_7=Python_3_2_Or_Later
> export PYD_D_VERSION_8=Python_3_3_Or_Later
> export PYD_D_VERSION_9=Python_3_4_Or_Later
> export PYD_D_VERSION_10=Python_3_5_Or_Later
> export PYD_D_VERSION_11=__PYD__DUMMY__
> export PYD_D_VERSION_12=__PYD__DUMMY__
> export PYD_D_VERSION_13=__PYD__DUMMY__
> export PYD_LIBPYTHON_DIR=/usr/lib
> export PYD_LIBPYTHON=python3.5m

This last one may well be the problem. Try manually adjusting it to
python3.5?

Having said this I tried export PYD_LIBPYTHON=python3.7 but it seems to
make no difference to not finding pandas.

> Where could be the problem and how can it be solved?

The problem seems to be that PyD is not looking inthe right place for
importing packages, but this is pure speculation.

Perhaps this merits a bug report against the PyD source repository?


-- 
Russel.
===========================================
Dr Russel Winder      t: +44 20 7585 2200
41 Buckmaster Road    m: +44 7770 465 077
London SW11 1EN, UK   w: www.russel.org.uk

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20190608/25abc3fb/attachment.sig>


More information about the Digitalmars-d-learn mailing list