Loading Python .pyd extensions

From BF2 Technical Information Wiki
Revision as of 19:24, 21 June 2018 by Pireax (talk | contribs) (Created page with "If you've found yourself on this page hoping to find out how to load your own C++ code (.DLL) via a .pyd file. You may well be dissappointed. As of yet I have not worked out h...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

If you've found yourself on this page hoping to find out how to load your own C++ code (.DLL) via a .pyd file. You may well be dissappointed. As of yet I have not worked out how to do this.

I was using the boost_python libraries to create the .pyd file, but I've downloaded several other types and you always get this error message.

ImportError: Don't know how to import XYZ (type code 3) 

If you know the answer please post here.

Perhaps this page can be about brain-storming a few ideas.

19 September 2006


I haven't used .pyd files or Boost.Python, so I don't have a solution for you, but I do have some suggestions:

  • Have you tried using your code with a normal stand-alone Python interpreter--that is, one that isn't part of BF2? The Python interpreter in BF2 is an old version of Python and has had some of it's standard libraries removed, so occasionally I've found things that work in normal Python that don't work in BF2. It may be that the ability to load .pyd files is one of the things that has been lobotomized out of BF2's Python.
  • I notice that the Boost.Python documentation only mentions compatibility with Python 2.2. BF2 uses Python 2.3--I don't know if this is a problem or not; maybe their documentation just hasn't been updated in a long time.
  • The Python/C API changes slightly from one version of Python to the next; if your .pyd file was compiled for the current version of Python (2.4), it probably won't work with BF2's Python (2.3). If this is the problem, you ought to be able to recompile for Python 2.3 and have it work.
  • Unless you really get into writing C code, you might have a look at Pyrex; it lets you write Python extensions in a subset of Python (a very large subset) that it then compiles into C code, which you can compile into a binary executable. It gives you the speed of C with the convienience of Python. I've been thinking about trying it with BF2, but haven't done so yet, so this is something of a shot in the dark.

--Woody 10:02, 19 September 2006 (MDT)


Thanx very much for the input, greatly appreciated. In reply to your points:

  • Yes I have run the .pyd from within an interpreter and it imports and runs perfectly.
  • good point, I will try compiling my .pyd against v2.2
  • I have tried compiling with both 2.3 and 2.4. BF2 python reports its version as 2.3
  • interesting suggestion, unfortunately I need to integrate existing c++ code and be able to call it from event handlers within BF2 so this is not going to solve my problem

What also puzzles me is that it is not just my .pyd which won't import into BF2 python, I have tried a wide variety of .pyd's from a number of sources and none will import ( ctypes, calldll, winsound among others). I am beginning to fear that what you said about the BF2 Python interpreter being lobotomized is the problem.

--Andrewd 01:59, 20 September 2006 (MDT)