Jump to content

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

      • Help Us Improve Cura – Join the Ultimaker Research Program
        🚀 Help Shape the Future of Cura and Digital Factory – Join Our Power User Research Program!
        We’re looking for active users of Cura and Digital Factory — across professional and educational use cases — to help us improve the next generation of our tools.
        Our Power User Research Program kicks off with a quick 15-minute interview to learn about your setup and workflows. If selected, you’ll be invited into a small group of users who get early access to features and help us shape the future of 3D printing software.

        🧪 What to Expect:
        A short 15-minute kickoff interview to help us get to know you If selected, bi-monthly research sessions (15–30 minutes) where we’ll test features, review workflows, or gather feedback Occasional invites to try out early prototypes or vote on upcoming improvements
        🎁 What You’ll Get:
         
        Selected participants receive a free 1-year Studio or Classroom license Early access to new features and tools A direct voice in what we build next
        👉 Interested? Please fill out this quick form
        Your feedback helps us make Cura Cloud more powerful, more intuitive, and more aligned with how you actually print and manage your workflow.
        Thanks for being part of the community,

        — The Ultimaker Software Team
        • 0 replies
      • Cura 5.10 stable released!
        The full stable release of Cura 5.10 has arrived, and it brings support for the new Ultimaker S8, as well as new materials and profiles for previously supported UltiMaker printers. Additionally, you can now control your models in Cura using a 3D SpaceMouse and more!
          • Like
        • 18 replies
    ×
    ×
    • Create New...