\ 跳到主要內容

Python importation

In a well-organized C++ code base, the hpp files should always be included using the full path like:

#include <modmesh/base.hpp>
#include <modmesh/profile.hpp>
#include <modmesh/buffer/buffer.hpp>

Python import 的module 並不必然要用python撰寫,也可以藉由C++。然而我誤解了這番話的意思,進而混用了 header files 與 modules,以為以下的實驗會成功:

import importation #succeed
import layer1.importation #succeed
import header #failed

以下是python importation 整理過後的資訊: 1. import modmesh

modmesh作為例子

import modmesh
我原先同樣是誤會 modmesh.hpp 才是被 python import的對象;但這個對象其實是library(or python package):$(MODMESH_ROOT)/modmesh。如果它已經被 built, 那麼目錄裡頭就會有 __init__.py 以及 .so 。

2. python importation

python importation 的機制在於編輯 sys.path. e,g 

del sys.path[0]
sys.path儲存的是尋找python module的路徑。在這個動作之後,原先若是被import的對象被列在sys.path[0]所儲存的路徑之下,那麼import就會失敗。
3. Module written in C++
如前所述,Python import 的module 並不必然要用python撰寫,e.g. C++, 它們是透過 import shared library(.so)來被python支援。 C++ and python 的連結可透過 swig, cython, pybind11 等等。在modmesh 中則是使用 pybind11。


留言