Issue 30446: Embedded 3.6.1 distribution cannot find _socket module (original) (raw)

Trying to import the socket module using the embedded distribution for 3.6.1, I get the error: <class 'ModuleNotFoundError'>: No module named '_socket'

Running with the standard interpreter works:

Python 3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 17:54:52) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

from socket import *

via an embedded C++ call, however, <class 'ModuleNotFoundError'>: No module named '_socket' is the error returned.

I've been meaning to get to looking at this issue for a couple of days now, sorry.

There are two critical pieces here:

The first one should be obvious - I assume you haven't messed that bit up :)

The second one is the "static" way of setting sys.path, as Zach suggests. By default, it should include 'python36.zip' and '.', which is the directory containing python36._pth and python36.dll. If you move any of these around, sys.path will be automatically inferred, which could break things.

Further, if you move/remove python36._pth, the registry and environment will be used to locate files rather than only looking in the application directory. This may be bad.

One final tip - you can rename python36._pth to your_exe_name._pth if you want, and if you are embedding then you can get roughly equivalent behaviour using PySys_SetPath.

If none of this works, we'll need you to provide some information about your application, especially the code used to initialize the interpreter, import socket, and the layout of all the files in your app. (Right now, you're asking us to guess basically every detail that is relevant to the bug, hence we're replying with "read the docs" and "try everything" rather than a specific fix ;) )

Thank you Steve and Zachary. The context you provided helped me track it down, so I have marked this closed. In case it benefits anyone in future as well, I ran into a subsequent topic where the C++ app was compiled in debug and hence the actual file being looked for was _socket_d.pyd which is not in the embedded distributable of course.

Thanks again for taking the time to provide your insights.

Thomas