Python throws a ModuleNotFoundError when loaded from C, but not when directly executed










1















I'm in the process of making a module called BSPy. It's not really relevant what the module does, but its setup.py is:



from distutils.core import setup, Extension

setup(name='BSPy', version='0.0.2', ext_modules=[Extension('BSPy', ['Interface.cpp'])])


Now, in order to do some debugging steps I'm using C to call a python file, and try to execute it using this bit of code:



#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;


So far so good. I first tested a hello world file to make sure it works, and this is working wonderfully. I then went on to change the python file to use this BSPy module (which executes fine on its own and from the command line). The file now looks like this:



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()))


And is supposed to give the output




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.



However, when I try and run the C script again, it suddenly throws out a 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'



Note that while it throws this error, pip is still confirming that BSPy is installed; hence I have no idea what could be wrong. Sadly, I can't give out a minimal working example, since the module can't be downloaded anywhere at the moment. I'm building it in this way though:



// 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;










share|improve this question

















  • 1





    It could be that the python extension library is using a wrong version of python, something like this: stackoverflow.com/questions/27408123/…

    – Milos Matovic
    Nov 14 '18 at 16:31











  • @MilosMatovic I read this post. I'm on windows though, and just to be sure, I removed the 2.7 version I've got installed. Either way, it shouldn't matter though, because the same instance of python is run for each line of python code, so the pip evaluation should list the same extension library. Am I mistaken?

    – Mitchell Faas
    Nov 14 '18 at 16:53







  • 1





    probably, your created exe hasn't the same python-path as the installed python. Check the output of import sys; print(sys.path) in your exe

    – ead
    Nov 16 '18 at 6:49











  • @ead You're right. It's using python_d.exe instead of python.exe. Do you know if there's any way to force it to use the right one?

    – Mitchell Faas
    Nov 16 '18 at 11:00















1















I'm in the process of making a module called BSPy. It's not really relevant what the module does, but its setup.py is:



from distutils.core import setup, Extension

setup(name='BSPy', version='0.0.2', ext_modules=[Extension('BSPy', ['Interface.cpp'])])


Now, in order to do some debugging steps I'm using C to call a python file, and try to execute it using this bit of code:



#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;


So far so good. I first tested a hello world file to make sure it works, and this is working wonderfully. I then went on to change the python file to use this BSPy module (which executes fine on its own and from the command line). The file now looks like this:



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()))


And is supposed to give the output




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.



However, when I try and run the C script again, it suddenly throws out a 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'



Note that while it throws this error, pip is still confirming that BSPy is installed; hence I have no idea what could be wrong. Sadly, I can't give out a minimal working example, since the module can't be downloaded anywhere at the moment. I'm building it in this way though:



// 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;










share|improve this question

















  • 1





    It could be that the python extension library is using a wrong version of python, something like this: stackoverflow.com/questions/27408123/…

    – Milos Matovic
    Nov 14 '18 at 16:31











  • @MilosMatovic I read this post. I'm on windows though, and just to be sure, I removed the 2.7 version I've got installed. Either way, it shouldn't matter though, because the same instance of python is run for each line of python code, so the pip evaluation should list the same extension library. Am I mistaken?

    – Mitchell Faas
    Nov 14 '18 at 16:53







  • 1





    probably, your created exe hasn't the same python-path as the installed python. Check the output of import sys; print(sys.path) in your exe

    – ead
    Nov 16 '18 at 6:49











  • @ead You're right. It's using python_d.exe instead of python.exe. Do you know if there's any way to force it to use the right one?

    – Mitchell Faas
    Nov 16 '18 at 11:00













1












1








1








I'm in the process of making a module called BSPy. It's not really relevant what the module does, but its setup.py is:



from distutils.core import setup, Extension

setup(name='BSPy', version='0.0.2', ext_modules=[Extension('BSPy', ['Interface.cpp'])])


Now, in order to do some debugging steps I'm using C to call a python file, and try to execute it using this bit of code:



#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;


So far so good. I first tested a hello world file to make sure it works, and this is working wonderfully. I then went on to change the python file to use this BSPy module (which executes fine on its own and from the command line). The file now looks like this:



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()))


And is supposed to give the output




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.



However, when I try and run the C script again, it suddenly throws out a 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'



Note that while it throws this error, pip is still confirming that BSPy is installed; hence I have no idea what could be wrong. Sadly, I can't give out a minimal working example, since the module can't be downloaded anywhere at the moment. I'm building it in this way though:



// 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;










share|improve this question














I'm in the process of making a module called BSPy. It's not really relevant what the module does, but its setup.py is:



from distutils.core import setup, Extension

setup(name='BSPy', version='0.0.2', ext_modules=[Extension('BSPy', ['Interface.cpp'])])


Now, in order to do some debugging steps I'm using C to call a python file, and try to execute it using this bit of code:



#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;


So far so good. I first tested a hello world file to make sure it works, and this is working wonderfully. I then went on to change the python file to use this BSPy module (which executes fine on its own and from the command line). The file now looks like this:



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()))


And is supposed to give the output




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.



However, when I try and run the C script again, it suddenly throws out a 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'



Note that while it throws this error, pip is still confirming that BSPy is installed; hence I have no idea what could be wrong. Sadly, I can't give out a minimal working example, since the module can't be downloaded anywhere at the moment. I'm building it in this way though:



// 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;







python c++ c python-c-api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 16:11









Mitchell FaasMitchell Faas

21418




21418







  • 1





    It could be that the python extension library is using a wrong version of python, something like this: stackoverflow.com/questions/27408123/…

    – Milos Matovic
    Nov 14 '18 at 16:31











  • @MilosMatovic I read this post. I'm on windows though, and just to be sure, I removed the 2.7 version I've got installed. Either way, it shouldn't matter though, because the same instance of python is run for each line of python code, so the pip evaluation should list the same extension library. Am I mistaken?

    – Mitchell Faas
    Nov 14 '18 at 16:53







  • 1





    probably, your created exe hasn't the same python-path as the installed python. Check the output of import sys; print(sys.path) in your exe

    – ead
    Nov 16 '18 at 6:49











  • @ead You're right. It's using python_d.exe instead of python.exe. Do you know if there's any way to force it to use the right one?

    – Mitchell Faas
    Nov 16 '18 at 11:00












  • 1





    It could be that the python extension library is using a wrong version of python, something like this: stackoverflow.com/questions/27408123/…

    – Milos Matovic
    Nov 14 '18 at 16:31











  • @MilosMatovic I read this post. I'm on windows though, and just to be sure, I removed the 2.7 version I've got installed. Either way, it shouldn't matter though, because the same instance of python is run for each line of python code, so the pip evaluation should list the same extension library. Am I mistaken?

    – Mitchell Faas
    Nov 14 '18 at 16:53







  • 1





    probably, your created exe hasn't the same python-path as the installed python. Check the output of import sys; print(sys.path) in your exe

    – ead
    Nov 16 '18 at 6:49











  • @ead You're right. It's using python_d.exe instead of python.exe. Do you know if there's any way to force it to use the right one?

    – Mitchell Faas
    Nov 16 '18 at 11:00







1




1





It could be that the python extension library is using a wrong version of python, something like this: stackoverflow.com/questions/27408123/…

– Milos Matovic
Nov 14 '18 at 16:31





It could be that the python extension library is using a wrong version of python, something like this: stackoverflow.com/questions/27408123/…

– Milos Matovic
Nov 14 '18 at 16:31













@MilosMatovic I read this post. I'm on windows though, and just to be sure, I removed the 2.7 version I've got installed. Either way, it shouldn't matter though, because the same instance of python is run for each line of python code, so the pip evaluation should list the same extension library. Am I mistaken?

– Mitchell Faas
Nov 14 '18 at 16:53






@MilosMatovic I read this post. I'm on windows though, and just to be sure, I removed the 2.7 version I've got installed. Either way, it shouldn't matter though, because the same instance of python is run for each line of python code, so the pip evaluation should list the same extension library. Am I mistaken?

– Mitchell Faas
Nov 14 '18 at 16:53





1




1





probably, your created exe hasn't the same python-path as the installed python. Check the output of import sys; print(sys.path) in your exe

– ead
Nov 16 '18 at 6:49





probably, your created exe hasn't the same python-path as the installed python. Check the output of import sys; print(sys.path) in your exe

– ead
Nov 16 '18 at 6:49













@ead You're right. It's using python_d.exe instead of python.exe. Do you know if there's any way to force it to use the right one?

– Mitchell Faas
Nov 16 '18 at 11:00





@ead You're right. It's using python_d.exe instead of python.exe. Do you know if there's any way to force it to use the right one?

– Mitchell Faas
Nov 16 '18 at 11:00












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304436%2fpython-throws-a-modulenotfounderror-when-loaded-from-c-but-not-when-directly-ex%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304436%2fpython-throws-a-modulenotfounderror-when-loaded-from-c-but-not-when-directly-ex%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党