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.7 stable released
        Cura 5.7 is here and it brings a handy new workflow improvement when using Thingiverse and Cura together, as well as additional capabilities for Method series printers, and a powerful way of sharing print settings using new printer-agnostic project files! Read on to find out about all of these improvements and more. 
         
          • Like
        • 16 replies
      • S-Line Firmware 8.3.0 was released Nov. 20th on the "Latest" firmware branch.
        (Sorry, was out of office when this released)

        This update is for...
        All UltiMaker S series  
        New features
         
        Temperature status. During print preparation, the temperatures of the print cores and build plate will be shown on the display. This gives a better indication of the progress and remaining wait time. Save log files in paused state. It is now possible to save the printer's log files to USB if the currently active print job is paused. Previously, the Dump logs to USB option was only enabled if the printer was in idle state. Confirm print removal via Digital Factory. If the printer is connected to the Digital Factory, it is now possible to confirm the removal of a previous print job via the Digital Factory interface. This is useful in situations where the build plate is clear, but the operator forgot to select Confirm removal on the printer’s display. Visit this page for more information about this feature.
        • 0 replies
    ×
    ×
    • Create New...