4

As a heads up, this question may appear to be a duplicate of

embedding python module error

Embedding Python 3 - no builtins?

but I think my issue is different. I'm running a Python 3.4 interpreter from an MSVC compiled C++ application, and I'm trying to use the ptvsd module to make debugging easier. So far I've been able to attach to python interpeters that I start from the command line without issue, but I'd like to attach to a python interpreter embedded in my C++ application. To do this, I've been following the advice of

https://github.com/Microsoft/PTVS/wiki/Cross-Platform-Remote-Debugging and http://blogs.msdn.com/b/cdndevs/archive/2014/10/16/part-5-get-started-with-python-debugging-in-ptvs.aspx

The ptvsd module imports the _socket module, which I can clearly see in C:/Python34/DLLs. If I invoke python34.exe from the command line and run

import ptvsd
ptvsd.enable_attach(None)

I can find and attach to the process by looking at tcp://localhost:5678.

However, if I call

PyRun_SimpleString("import ptvsd");

from C++, I get an error saying that the _socket module could not be found. The same seems to be true for importing any of the builtin python modules into my C++ application, although I am able to import them correctly from a command line invoked python interpreter.

I am able to execute

PyRun_SimpleString("import sys \n print(sys.path)");

from my C++ application, and the result does show C:/Python34/DLLs, where the _socket.pyd file is located. But for some reason I'm unable to pick it up when I try and import it, or import ptvsd

Following the advice of Embedding Python 3 - no builtins?, I ran

PyObject* pGlobals = PyDict_New();
PyRun_String("import ptvsd", Py_file_input, pGlobals, pGlobals);
PyRun_String("ptvsd.enable_attach(None)", Py_file_input, pGlobals, pGlobals);

Which is a command that I truly don't understand. It actually made the error about _socket not being found vanish, but I think it just suppressed it. Calling

dir(ptvsd)

from python does display its functionality, but calling

PyRun_SimpleString("print(dir(ptvsd))");

does not. The first SO link I posted deals with a hand built module that couldn't be picked up by the interpeter, but that's not the case here. The module lives in a place that PYTHONPATH can find it, and it gets picked up by the command line fine (it also gets picked up by the Python Tools for Visual Studio IDE, but that's beside the point.)

I'm unable to check python2.7 on windows, but on linux python (2 and 3) had no trouble importing the _socket module from the c++ interpreter, so I'm hoping that it's just an environment issue.

Additionally, the output of

print(sys.version)

is

3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)]

from the command line, and

3.4.3 (default, Aug 29 2015, 22:43:06) [MSC v.1800 32 bit (Intel)]

from my C++ interpreter.

Sorry for all that, but does anyone understand what's going wrong? I'm sure there are some other hoops I need to go through to get things to work, but I don't know what they are yet...

For what it's worth, I'm able to import sys and math just fine. I'm also able to import a custom module I compile via

PyImport_AppendInittab(ModuleName.c_str(), _Mod_Init);

And I've tried all my tests with and without the above call.

Thank you for your time.

  • John
Community
  • 1
  • 1
mynameisjohnj
  • 421
  • 4
  • 12

0 Answers0