chengcj
chengcj

Reputation: 928

How to import a .pyd file as a python module?

I am using PyCharm. I have a python script in the following location:

C:\Users\XYZ\PycharmProjects\Project1\playground.py

playground.py only has a line of code as shown below:

import PyTbl

In the Project1 folder there's another file:

C:\Users\XYZ\PycharmProjects\Project1\PyTbl.pyd

When I run the Python script playground.py I get the following error:

ImportError: numpy.core.multiarray failed to import
Traceback (most recent call last):
  File "C:/Users/XYZ/PycharmProjects/Project1/playground.py", line 1, in <module>
    import PyTbl
SystemError: initialization of PyTbl raised unreported exception

If I hover my mouse over the line of Python code in playground.py in the PyCharm editor I get the following error message:

"No module named PyTbl"

Any idea how should I import a .pyd file into a Python script?

Upvotes: 19

Views: 88103

Answers (4)

shui shui
shui shui

Reputation: 21

E.g. If 'test.pyd' is present in the current directory.

main.py

import sys

# Add the current directory to sys.path
sys.path.append('.')
import test

In this way, test.pyd can be loaded and used, and 2023.12.1 runs successfully in pycharm!

Upvotes: 2

KonArtist
KonArtist

Reputation: 129

If you have a DLL named foo.pyd, then it must have a function PyInit_foo(). You can then write Python “import foo”, and Python will search for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, will attempt to call PyInit_foo() to initialize it. You do not link your .exe with foo.lib, as that would cause Windows to require the DLL to be present.

Note that the search path for foo.pyd is PYTHONPATH, not the same as the path that Windows uses to search for foo.dll. Also, foo.pyd need not be present to run your program, whereas if you linked your program with a dll, the dll is required. Of course, foo.pyd is required if you want to say import foo. In a DLL, linkage is declared in the source code with __declspec(dllexport). In a .pyd, linkage is defined in a list of available functions.

https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll

Upvotes: 0

Benyamin Jafari
Benyamin Jafari

Reputation: 34146

.pyd is basically a Windows .dll file.

.pyd files are dll’s, but there are a few differences. If you have a DLL named foo.pyd, then it must have a function PyInit_foo(). You can then write Python import foo, and Python will search for foo.pyd (as well as foo.py, foo.pyc) and if it finds it, will attempt to call PyInit_foo() to initialize it. You do not link your .exe with foo.lib, as that would cause Windows to require the DLL to be present.

Note that the search path for foo.pyd is PYTHONPATH, not the same as the path that Windows uses to search for foo.dll. Also, foo.pyd need not be present to run your program, whereas if you linked your program with a dll, the dll is required. Of course, foo.pyd is required if you want to say import foo. In a DLL, the linkage is declared in the source code with __declspec(dllexport). In a .pyd, linkage is defined in a list of available functions.

Reference

Upvotes: 17

&#193;tila Neves
&#193;tila Neves

Reputation: 1412

One thing that will work for sure is to set the PYTHONPATH environment variable before launching PyCharm to include the directory where the .pyd file is. I don't know if there's a simpler way of doing this within PyCharm itself.

Upvotes: 4

Related Questions