Jump to content
Ultimaker Community of 3D Printing Experts

Import library inside plugin (but it's cpython)


Tyronnosaurus
 Share

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
     Share

    • Our picks

      • New here? Get ahead with a free onboarding course
        Hi,
         
        Often getting started is the most difficult part of any process. A good start sets you up for success and saves you time and energy that could be spent elsewhere. That is why we have a onboarding course ready for
        Ultimaker S5 Pro Bundle, Ultimaker S5, Ultimaker S3 Ultimaker 2+ Connect.   
        They're ready for you on the Ultimaker Academy platform. All you need to do to gain access is to register your product to gain free access. 
        Ready? Register your product here in just 60 seconds.
          • Like
        • 11 replies
    ×
    ×
    • Create New...