ImportError while importing a python file in a pyd

2019-03-02 02:28发布

问题:

Alright, so, what I'm trying to do is import a module in folders packed inside of a .pyd file. Here is something that would work for me:

from apple import __init__

With apple being the .pyd in the same directory as the Python script, and __init__ of course being packed inside of the .pyd. This would work, however here is what I want to do, but doesn't work:

from apple.seed.worm import WormManager

Explanation: apple = pyd, seed = directory in the pyd, worm = directory in seed directory in apple pyd, WormManager = python module in the worm directory.

However, it does not work, and results in a module not found ImportError thinking that seed is a module (and there was an __init__ inside of the seed directory before it was packed). Of course it exists and is packed in the .pyd, but it simply does not work. I even did this:

from apple.seed import __init__

but not even that works so I know that I'm not importing this right.

I really could not find the correct syntax for getting this to work on the internet, and of course I know how to do this without a pyd involved, so any help?

回答1:

You need to run C:\path\to\pip\tool\pip install pyd from the command line first.



回答2:

I'm not sure but I think that Python cannot dynamically discover internal pyd API without importing it first.

You should try to import the pyd first:

import apple

Then you can (probably) access the internal API:

WM = apple.seed.worm.WormManager

Also, your apple.pyd dll file must contain a function PyInit_apple() per the official documentation. If it doesn't, or if it is ill-defined (doesn't define all the submodules), this may explain why you cannot do from apple ....