18

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?

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
chengcj
  • 888
  • 2
  • 8
  • 22
  • A .pyd python file is basically a windows dll file, I don't think you can import it because of this (It's compiled python code). I'm unsure what this file contains, if it needs to be compiled to readable python, but you could try changing the extension to .py instead of .pyd. – Snuffles May 10 '18 at 17:17
  • Do you have `__init__.py` file in the directory? This is a empty file that is needed to import .py files that aren't in the default libraries. – NaruS May 10 '18 at 17:28
  • 3
    Your import statement is correct. That is how you import a `.pyd` file. But it isn't Python, it is object code, probably originally C. If the DLL doesn't load, that usually because (1) it is trying to load another DLL that is missing or not findable; or (2) you have a 32-bit system and your code is trying to load a 64-bit DLL, or *vice versa*. – BoarGules May 10 '18 at 20:58
  • @NaruS: Yes I have `__init__.py` in the directory as well. – chengcj May 11 '18 at 08:02

3 Answers3

14

.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

Community
  • 1
  • 1
Benyamin Jafari
  • 27,880
  • 26
  • 135
  • 150
3

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.

Átila Neves
  • 1,351
  • 11
  • 14
  • 2
    Brilliant! Confirmed works well with PyCharm 2020.2. Remember to restart PyCharm after setting the environment variable. – Contango Oct 30 '20 at 17:43
  • In addition, since I used python boost I needed to do a some more tweaks: **1.** Python Boost 1.7.6 works with Python 3.9.x - Installed python 3.9.x **2.** In PyCharm project settings, set the interpreter as Phython 3.9 **3.** In Visual Studio - make the output file as pyd ($(OutDir)$(TargetName)$(TargetExt) --> $(OutDir)$(TargetName).pyd) - this is nice to have, to avoid manual rename **4.** The name of the pyd must be the same as the name of the package you want to import (I.e. PyInit_, e.g. "MyPackage.pyd" should have an exposed PyInit_MyPackage). **5.** ... – Juv Jul 11 '21 at 09:59
-1

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

KonArtist
  • 109
  • 2
  • 9