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

      • 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.
          • Like
        • 0 replies
      • Ultimaker Cura 5.6 stable released
        Cura now supports Method series printers!
         
        A year after the merger of Ultimaker and MakerBotQQ, we have unlocked the ability for users of our Method series printers to slice files using UltiMaker Cura. As of this release, users can find profiles for our Method and Method XL printers, as well as material profiles for ABS-R, ABS-CF, and RapidRinse. Meaning it’s now possible to use either Cura or the existing cloud-slicing software CloudPrint when printing with these printers or materials
        • 48 replies
    ×
    ×
    • Create New...