Jump to content

Import library inside plugin (but it's cpython)


Tyronnosaurus

Recommended Posts

Posted (edited) · Import library inside plugin (but it's cpython)

Some time ago I made a python application which I am now integrating into a Cura plugin.  It uses the h5py library, which is usually installed through pip.

 

I assumed that copying the source code and using an 'import h5py' would be enough, but nope: the library is written in cpython, and when installed with pip it actually compiles into a bunch of dll and pyd files:

 

InstalledH5py.thumb.PNG.aa9240c245f029ed5825867884aaada3.PNG

 

I've unsuccessfully tried many ways to import it from this forum (1, 2), from StackOverflow (1), etc.

Most methods involved copying the files compiled for my system, and loading them with ctypes or adding them to Path.

 

The following method manages to import it without errors, but as soon as I use one of its classes it tells me it doesn't exist:

import os
from ctypes import *

def LoadHDF5Data(filetoload):

    #We need to chdir because otherwise it will fail to find many dependencies
    os.chdir("C:\\Users\\myname\\AppData\\Roaming\\cura\\4.7\\plugins\\FemPlugin\\ResultsNode\\h5py")

    path = "C:\\Users\\myname\\AppData\\Roaming\\cura\\4.7\\plugins\\FemPlugin\\ResultsNode\\h5py\\hdf5_hl.dll"
    h5py = cdll.LoadLibrary(path)  #This replaces the usual "import h5py". h5py is now a usable module

    f = h5py.File(filetoload, 'r')    #Crashes Cura:  "AttributeError: function 'File' not found"

 

Anyone knows what the correct approach is?

 

 

 

Edited by Tyronnosaurus
  • Link to post
    Share on other sites

    Posted · Import library inside plugin (but it's cpython)

    I noticed that the compiled files were made for Python 3.8. Since Cura comes with Pyton 3.5.0, I repeated the process but this time using h5py compiled in a Windows system with Python 3.5.0 installed.

     

    This time, Cura crashes with this error:

    [...]
        h5py = cdll.LoadLibrary(path)  #This replaces the usual "import h5py". h5py is now a usable module
      File "ctypes\__init__.py", line 429, in LoadLibrary
      File "ctypes\__init__.py", line 351, in __init__
    OSError: [WinError 193] %1 is not a valid Win32 application

     

  • Link to post
    Share on other sites

    Posted · Import library inside plugin (but it's cpython)

    nallath's observation saved my skin: h5py's latest version requires Python 3.6, so it can't run on Cura (Python 3.5.x). However, a previous version (2.10.0) is still compatible.

     

    I've prepared a clean Windows 10 install (on a virtual machine to prevent any mix-up with different Python versions), There, I've installed the same Python version as Cura (3.5.4, 64 bit), pip-installed h5py 2.10.0, and copied the folder with the compiled files:

      C:/Users/xxxx/AppData/Local/Programs/Python/Python35/Lib/site-packages/h5py

      (might be Roaming instead of Local for some installs)

     

    This folder can now be used as a Python package. For some reason, a simple import fails so I have to do this:

    import os, sys
    dir = os.path.dirname(os.path.realpath(__file__)) #This script's path
    sys.path.append(dir)	#Look for packages here
    import h5py
    
    f = h5py.File(testFile,"r")

     

    I still need to ensure an old h5py version suits my needs, and later repeat the process for Linux and macOS.

     

  • Link to post
    Share on other sites

    Posted · Import library inside plugin (but it's cpython)
    5 hours ago, Tyronnosaurus said:

    I've prepared a clean Windows 10 install (on a virtual machine to prevent any mix-up with different Python versions)

    Learn to love python virtualenv.

  • Link to post
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    • Our picks

      • UltiMaker Cura 5.8 Stable released 🎉
        In the Cura 5.8 stable release, everyone can now tune their Z seams to look better than ever. Method series users get access to new material profiles, and the base Method model now has a printer profile, meaning the whole Method series is now supported in Cura!
        • 5 replies
      • Introducing the UltiMaker Factor 4
        We are happy to announce the next evolution in the UltiMaker 3D printer lineup: the UltiMaker Factor 4 industrial-grade 3D printer, designed to take manufacturing to new levels of efficiency and reliability. Factor 4 is an end-to-end 3D printing solution for light industrial applications
          • Thanks
          • Like
        • 3 replies
    ×
    ×
    • Create New...