2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.pydファイルは、WindowsではDLLそのもの

Last updated at Posted at 2020-12-30

.pydファイルは、WindowsではDLLそのもの、とのことで、勉強になりました。

WindowsでPybind11を使ってC++で自作した関数をPythonにインポートする際は、C++でDLLを作成するタイプのVisual Studioプロジェクトを作成し、PYBIND11_PLUGIN(...){ ... }を含むコードを付加、ビルド、DLLを生成、生成したDLLの名前を.dll→.pydに変更して、.pyスクリプトファイルと同じフォルダに.pydを配置、.pyスクリプト内からファイル名でimport可能、という仕組みのよう。

(.pydのdは、DLLのd? かなと思ってしまいますが...それなら.pydllと名付ける方が分かり易いはずですが...、ただ、Windows限定の話になってしまうので、違うのかな、など悶々...)

以下引用:


Pybind11を使ってC ++をPythonに拡張する
https://www.366service.com/jp/qa/9c02528547c8067355edf43854e3fd4e

Pythonでは、.pydファイルの名前は、内部のモジュールと同じでなければなりません。ドキュメント( https://docs.python.org/2/faq/windows.html )から:

foo.pydという名前のDLLがある場合、関数initfoo()が必要です。次に、Python "import foo"を書くことができ、Pythonはfoo.pyd(foo.py、foo.pycと同様)を検索し、見つかった場合はinitfoo()を呼び出して初期化しようとします。


PythonからC++のクラスを使ってみるテスト(pybind11版)
https://qiita.com/tibigame/items/499a81f0e1cb47962b90

ビルド後に**「ClassTestLib.dll」を「ClassTestLib.pyd」にリネーム**して
pythonコードがあるディレクトリにコピーする。


Python on Windows FAQ
https://docs.python.org/2/faq/windows.html#is-a-pyd-file-the-same-as-a-dll

Is a *.pyd file the same as a DLL?

Yes, .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 initfoo(). 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 initfoo() to initialize it. You do not link your .exe with foo.lib, as that would cause Windows to require the DLL to be present.

DeepLで翻訳↓

.pydファイルはDLLと同じですか?

はい、.pyd ファイルは DLL ですが、いくつかの違いがあります。もし foo.pyd という名前の DLL があれば、それは initfoo() 関数を持っていなければなりません。Python に "import foo" と書けば、Python は foo.pyd (および foo.py, foo.pyc) を検索し、それを見つけたら initfoo() を呼び出して初期化を試みます。.exe と foo.lib をリンクしてはいけません。

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?