3

On windows I have built a very simple "hello world" C extension (the file hello.c from this site https://gist.github.com/physacco/2e1b52415f3a964ad2a542a99bebed8f). Using VS2015 I successfully obtain hello.dll. The problem is that I can't figure out how to import this file/module.

In the python shell (python 3.7) I have made sure that I'm in the same folder as the hello.dll. I have also made sure that sys.path() contains the folder path. But when I write "import hello" I get an error "ModuleNotFoundError: No module named 'hello'"

Does anyone has an idea of what is wrong is this very simple setup?

Update: When trying to import a module that does not exist the ModuleNotFoundError is reported. After renaming the hello.dll to hello.pyd an ImportError is returned. So it seems like it tries to actually load the module.

cosas
  • 43
  • 5
  • Did you execute `python3 setup.py build`? – dvlper Jul 06 '20 at 14:21
  • @dvlper No I built i manually in VS2015 – cosas Jul 06 '20 at 15:54
  • 1
    From what I understand; you built on VS2015, and obtained the (.dll). Now on Python side, you need to set it up as an Extension (see: distutils.core.Extension), for which you'll have to execute `python3 setup.py build`. FYI: this `setup.py` file is available in the same link you posted. – dvlper Jul 06 '20 at 15:59
  • @dvlper I know it may seem strange, but I'm trying to avoid using setup.py. And because the example is so simple I hoped it would be easy to just use the module without running other tools. – cosas Jul 06 '20 at 16:19

1 Answers1

4

Python compiled modules on Windows have the extension .pyd, not .dll. If you'd built it using setup.py the file would be built with the correct name. However, you built it yourself and gave it a name that Python doesn't recognise as a module.


In terms of the build command: you have to link it with libpython. You don't look to be doing this. The error you report is definitely one that you can get if the module is not linked against all its dependencies.

I know you don't want to use setup.py, however I'd use it at least once just to see what it does and if it works. You'll then at least have a command that you can copy with a working set of options.

DavidW
  • 29,336
  • 6
  • 55
  • 86
  • Ah, I see. When I rename hello.dll to hello.pyd I get a different error ">>> import hello Traceback (most recent call last): File "", line 1, in ImportError: DLL load failed: The specified module could not be found." – cosas Jul 06 '20 at 16:15
  • @DawidW It's now an import error instead of ModuleNotFoundError. Not sure what that means. – cosas Jul 06 '20 at 16:16
  • 2
    It's found the DLL but something else is wrong - it's failing at a later stage of the process. – DavidW Jul 06 '20 at 16:23
  • Yes, I just tried to delete everything in the folder and the ModuleNotFoundError is reported. When compiling and renaming the .dll to .pyd the ImportError is reported. – cosas Jul 06 '20 at 16:24
  • 1
    I'd suggest that the _exact_ command you used to build it would probably be the next thing to look at. – DavidW Jul 06 '20 at 17:58
  • Yes, I looked at the build options, /GS /W3 /Zc:wchar_t /ZI /Gm /Od /sdl /Fd"x64\Debug\vc140.pdb" /Zc:inline /fp:precise /D "_WINDLL" /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /RTC1 /Gd /MTd /Fa"x64\Debug\" /EHsc /nologo /Fo"x64\Debug\" /Fp"x64\Debug\hello.pch" , but didn't ring a bell to me.. – cosas Jul 06 '20 at 18:14
  • 1
    How can the command you used to build it "not ring a bell"? Is something else generating the build options? This is all stuff that should be in the question... – DavidW Jul 06 '20 at 18:47
  • Not sure I follow. It is just the 'standard' debug build options in VS. I just made sure it was x64 to match my python install. Don't see how any if the build options can make dll load fail. – cosas Jul 06 '20 at 18:59
  • Just saw you edited your answer. Good points. I just showed the compiler options not the linker options. I'm linking against "python35_d.lib" otherwise error is thrown. But it is a good idea to try setup.py once to make sure it can install. – cosas Jul 06 '20 at 19:05
  • 1
    Python can be slightly picky about how you build it so it's worth starting from a known working point. If you're linking against Python 3.5 but trying to run in Python 3.7 that's definitely wrong though! The version does need to match. – DavidW Jul 06 '20 at 19:06
  • Thanks for your help. I finally found out what was wrong. The name of the file must end with "_d". So if the name is hello_d.py then it loads and works as expected. – cosas Jul 06 '20 at 19:43