我正在制作一个名为BSPy的模块。模块的功能并不重要,但是其setup.py
是:
from distutils.core import setup, Extension
setup(name='BSPy', version='0.0.2', ext_modules=[Extension('BSPy', ['Interface.cpp'])])
现在,为了执行一些调试步骤,我使用C调用python文件,并尝试使用以下代码执行该文件:
#include<Python.h>
int main(int argc, char **argv) {
wchar_t *program = Py_DecodeLocale(argv[0], NULL);
if (program == NULL) {
fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
exit(1);
}
Py_SetProgramName(program);
Py_Initialize();
FILE* PythonScriptFile = fopen("D:/- Coding/python-bsp/test.py", "r");
if (PythonScriptFile)
{
PyRun_SimpleFile(PythonScriptFile, "D:/- Coding/python-bsp/test.py");
fclose(PythonScriptFile);
}
Py_Finalize();
getchar();
return 0;
到目前为止,一切都很好。我首先测试了一个hello world文件,以确保它可以正常工作,并且效果很好。然后,我继续更改python文件以使用此BSPy模块(该模块可以自己执行,也可以从命令行执行)。文件现在看起来像这样:
import platform
import pip # needed to use the pip functions
print(platform.python_version())
print(sorted(["%s==%s" % (i.key, i.version) for i in pip.get_installed_distributions()]))
import BSPy as bsp
print("Hello world! - from processor {} of {}.".format(bsp.pid(), bsp.nprocs()))
应该提供输出
3.6.5 ['absl-py==0.2.2', 'argh==0.26.2', 'astor==0.6.2', 'astroid==1.6.4', 'autopep8==1.3.5', 'bleach==1.5.0', 'browser-cookie3==0.6.4', 'browsercookie2==0.7.1', 'bspy==0.0.2', 'cachetools==2.1.0', 'certifi==2018.4.16', 'chardet==3.0.4', 'colorama==0.3.9', 'cycler==0.10.0', 'entrypoints==0.2.3', 'feedparser==5.2.1', 'flake8-docstrings==1.3.0', 'flake8-polyfill==1.0.2', 'flake8==3.5.0', 'future==0.16.0', 'gast==0.2.0', 'google-api-core==1.2.0', 'google-auth==1.5.0', 'google-cloud-core==0.28.1', 'google-cloud-logging==1.6.0', 'googleapis-common-protos==1.5.3', 'grpcio==1.12.0', 'gtp==0.2', 'h5py==2.8.0', 'html5lib==0.9999999', 'httplib2==0.11.3', 'idna==2.6', 'iso8601==0.1.12', 'isort==4.3.4', 'jedi==0.13.1', 'keras-applications==1.0.6', 'keras-preprocessing==1.0.5', 'keyring==10.3.2', 'kiwisolver==1.0.1', 'lazy-object-proxy==1.3.1', 'lxml==4.2.5', 'lz4==2.0.0', 'm3u8==0.3.5', 'markdown==2.6.11', 'matplotlib==2.2.2', 'mccabe==0.6.1', 'mymodule==1.0', 'numpy==1.14.3', 'oauth2client==4.1.0', 'pandas==0.23.0', 'parso==0.3.1', 'pbkdf2==1.3', 'petname==2.2', 'pillow==5.3.0', 'pip==9.0.3', 'pluggy==0.7.1', 'protobuf==3.5.2.post1', 'pyaes==1.6.1', 'pyasn1-modules==0.2.1', 'pyasn1==0.4.3', 'pybsp==0.0.1', 'pycodestyle==2.3.1', 'pycryptodome==3.6.1', 'pydocstyle==2.1.1', 'pyflakes==1.6.0', 'pylint==1.9.1', 'pyparsing==2.2.0', 'python-dateutil==2.7.3', 'python-jsonrpc-server==0.0.2', 'python-language-server==0.21.2', 'pytz==2018.4', 'pywin32-ctypes==0.1.2', 'requests==2.18.4', 'rope==0.11.0', 'rsa==3.4.2', 'setuptools==40.4.3', 'sgf==0.5', 'six==1.11.0', 'snowballstemmer==1.2.1', 'tensorboard==1.8.0', 'tensorflow-gpu==1.8.0', 'termcolor==1.1.0', 'tqdm==4.23.4', 'typed-ast==1.1.0', 'urllib3==1.22', 'virtualenv==16.1.0', 'webencodings==0.5.1', 'werkzeug==0.14.1', 'wheel==0.31.1', 'wordcloud==1.5.0', 'wrapt==1.10.11', 'yapf==0.24.0'] Hello world! - from processor 3735928559 of 24.
但是,当我尝试再次运行C脚本时,它突然抛出了ModuleNotFoundError:
3.6.5 ['absl-py==0.2.2', 'argh==0.26.2', 'astor==0.6.2', 'astroid==1.6.4', 'autopep8==1.3.5', 'bleach==1.5.0', 'browser-cookie3==0.6.4', 'browsercookie2==0.7.1', 'bspy==0.0.2', 'cachetools==2.1.0', 'certifi==2018.4.16', 'chardet==3.0.4', 'colorama==0.3.9', 'cycler==0.10.0', 'entrypoints==0.2.3', 'feedparser==5.2.1', 'flake8-docstrings==1.3.0', 'flake8-polyfill==1.0.2', 'flake8==3.5.0', 'future==0.16.0', 'gast==0.2.0', 'google-api-core==1.2.0', 'google-auth==1.5.0', 'google-cloud-core==0.28.1', 'google-cloud-logging==1.6.0', 'googleapis-common-protos==1.5.3', 'grpcio==1.12.0', 'gtp==0.2', 'h5py==2.8.0', 'html5lib==0.9999999', 'httplib2==0.11.3', 'idna==2.6', 'iso8601==0.1.12', 'isort==4.3.4', 'jedi==0.13.1', 'keras-applications==1.0.6', 'keras-preprocessing==1.0.5', 'keyring==10.3.2', 'kiwisolver==1.0.1', 'lazy-object-proxy==1.3.1', 'lxml==4.2.5', 'lz4==2.0.0', 'm3u8==0.3.5', 'markdown==2.6.11', 'matplotlib==2.2.2', 'mccabe==0.6.1', 'mymodule==1.0', 'numpy==1.14.3', 'oauth2client==4.1.0', 'pandas==0.23.0', 'parso==0.3.1', 'pbkdf2==1.3', 'petname==2.2', 'pillow==5.3.0', 'pip==9.0.3', 'pluggy==0.7.1', 'protobuf==3.5.2.post1', 'pyaes==1.6.1', 'pyasn1-modules==0.2.1', 'pyasn1==0.4.3', 'pybsp==0.0.1', 'pycodestyle==2.3.1', 'pycryptodome==3.6.1', 'pydocstyle==2.1.1', 'pyflakes==1.6.0', 'pylint==1.9.1', 'pyparsing==2.2.0', 'python-dateutil==2.7.3', 'python-jsonrpc-server==0.0.2', 'python-language-server==0.21.2', 'pytz==2018.4', 'pywin32-ctypes==0.1.2', 'requests==2.18.4', 'rope==0.11.0', 'rsa==3.4.2', 'setuptools==40.4.3', 'sgf==0.5', 'six==1.11.0', 'snowballstemmer==1.2.1', 'tensorboard==1.8.0', 'tensorflow-gpu==1.8.0', 'termcolor==1.1.0', 'tqdm==4.23.4', 'typed-ast==1.1.0', 'urllib3==1.22', 'virtualenv==16.1.0', 'webencodings==0.5.1', 'werkzeug==0.14.1', 'wheel==0.31.1', 'wordcloud==1.5.0', 'wrapt==1.10.11', 'yapf==0.24.0'] Traceback (most recent call last): File "D:/- Coding/python-bsp/test.py", line 8, in <module> import BSPy as bsp ModuleNotFoundError: No module named 'BSPy'
请注意,尽管抛出此错误,但pip仍在确认已安装BSPy;因此我不知道有什么问题。可悲的是,我无法给出一个最小的工作示例,因为该模块目前无法在任何地方下载。我是用这种方式构建的:
// Declare functions
extern "C" {
static PyObject* init(PyObject* self, PyObject* args);
static PyObject* begin(PyObject* self, PyObject* args);
static PyObject* end(PyObject* self, PyObject* args);
static PyObject* execute(PyObject* self, PyObject* args);
static PyObject* pid(PyObject* self, PyObject* args);
static PyObject* nprocs(PyObject* self, PyObject* args);
}
// Define the methods with docstrings
static PyMethodDef myMethods[] = {
{"init", init, METH_VARARGS, "Runs SPMD function" },
{"begin", begin, METH_VARARGS, "Spawns an amount of processes"},
{"end", end, METH_NOARGS, "Ends the processes"},
{"execute", execute, METH_VARARGS, "Execute function"},
{"pid", pid, METH_NOARGS, "Returns processor ID"},
{"nprocs", nprocs, METH_NOARGS, "Returns number of processors."},
{NULL, NULL, 0, NULL}
};
// Define the module
static struct PyModuleDef BSPy = {
PyModuleDef_HEAD_INIT,
"BSPy",
"A Python implementation of BSP",
-1,
myMethods
};
// Initialize the module
PyMODINIT_FUNC PyInit_BSPy(void) {
return PyModule_Create(&BSPy);
}
///* Goes on to define each function. Only the simplest example is shown here.*///
static PyObject* end(PyObject* self, PyObject* args) {
bsp_end();
return Py_None;
}