Upgrading plugins to CellProfiler 4

Thanks, Beth.
This is really helpful. However, I still get some errors:

  • ValueError: Could not find cellprofiler_core.module.Module class
  • ModuleNotFoundError: No module named ‘cellprofiler_core.icons’

Maybe this is out of the scope for this issue, but when I try to recompile some C-code for my plugins for Python 3, I get errors of the following type:
error LNK2001: unresolved external symbol __imp__PySequence_GetItem
It seems like __imp is deprecated. Do you have any hints on this?

I managed to get one step further.
I managed to build the c-file (I was using the wrong VS Command prompt – x86 instead of x64), but I still have some issues: when starting CellProfiler I get the following error message
ModuleNotFoundError: No module named …

I will continue to dig into this.

Glad to hear the C compilation got fixed!

Icons wouldn’t be in cellprofiler_core, since they’re only needed for the GUI (rule of thumb- cellprofiler_core is all the logic you need to load images and run headless, cellprofiler contains the modules and the GUI elements).

What about the cellprofiler_core.module.Module? Where can I find that?

(Moving to a new thread here)

cellprofiler_core.module.Module exists; here’s a plugin that successfully calls it. Not sure why you’re not seeing it; maybe something weird in your environment?

Also, in case I haven’t passed this along to you yet, here’s our 3->4 plugin upgrade guide; feel free to add to it if you think there’s anything missing.

(Also moving my replies)
I still have two different issues with my plugins. Both occur when I start CellProfiler4.

  1. ValueError: Could not find cellprofiler_core.module.Module class in C:\CellProfilerPlugins\pluginsCP4\MyPlugin.py

In the file MyPlugin.py, I have a class declaration
class MyPlugin(cellprofiler_core.module.Module):

  1. I have built a file mycfunction.cp36-win_amd64.pyd from mycfunction.c.
    In UseMyCFunction.py, I try to import
    import mycfunction
    but then I get an error
    ModuleNotFoundError: No module named ‘mycfunction’
    or if I compile it so that the name is mycfunction.pyd (without cp36-win_amd64) then I get the following error message:
    ImportError: DLL load failed while importing mycfunction: The specified module could not be found.

Regarding point 1, could you try naming the plugin myplugin.py rather than MyPlugin.py? I’ve a feeling that the new plugin loader may be more case-sensitive than intended. If that works I might tweak the loader to be less fussy in the next version!

Regarding point 2, where are you placing the pyd file?

Hi David.

  1. Yes, it worked when I renamed it to myplugin.py. Thanks!
  2. I am placing the pyd file in the same folder as the corresponding py file, i.e., C:\CellProfilerPlugins\pluginsCP4\

Another error is:
AttributeError: module ‘cellprofiler_core.measurement’ has no attribute ‘IMAGE’

Yeah, most (if not all) of the aliased all-caps variables have been removed, as it makes program logic super challenging (we were getting into cases where imports were becoming loops, all because one module was importing another to get access to its aliased variables- IMAGE is now just Image, etc.

I still get AttributeError: module ‘cellprofiler_core.measurement’ has no attribute ‘Image’

Are you still trying to do imports? That won’t work since the variables are no longer assigned, you can either manually set any variables like IMAGE that the rest of your code uses, or do a find and replace for the variable IMAGE to just set to the string “Image”

OK, that seemed to work. Thanks!

1 Like

Regarding the DLL loading issue, Python 3.8 on Windows is a bit fussy. If you’re still having trouble it might be worth adding the following before running the import statement:

import os
os.add_dll_directory("/path/to/folder/with/c-file")

If that works, you should be able to grab the correct directory using the plugin directory setting fetching function from cellprofiler_core.

I am not really sure what you mean here. My problem is that the .pyd file is not found. Which directory should I use in add_dll_directory - the CellProfiler plugins directory, or the directory where I have t original c-code?

It should be whichever directory the compiled c-code is stored in (the mycfunction.cp36-win_amd64.pyd file you mentioned earlier). As of Python 3.8 you need to explicitly tell it where to search for DLL/pyd files, so depending on where you stored the file you may need to add that directory using the code I outlined.

I don’t get this to work. I have tried both to add the path to the CellProfilerPlugins (where I have put a copy of the pyd file) or the folder where I have the original c file and the compiled pyd file.

Thanks for trying that, unfortunately there’s not much more we can do unless you’d be able to upload the plugin files?

Hi David,
Here is one example of a plugin where I try to import C-code
SizeIntervalPrecision.zip (119.8 KB)