Can't find 'socket'

Just downloaded/installed latest Python (3.13) after four hours trying to get a script to run (with help (?) from gpt.
And it still won’t go.
PS C:\ytdlp> python.exe substackgetvid.py

Traceback (most recent call last):
File “C:\ytdlp\substackgetvid.py”, line 3, in
import requests
File “C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\requests_init_.py”, line 43, in
import urllib3
File “C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3_init_.py”, line 14, in
from . import exceptions
File “C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\exceptions.py”, line 3, in
import socket
File “C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\socket.py”, line 52, in
import _socket
ImportError: DLL load failed while importing _socket: The specified module could not be found.

so I tried installing ‘socket’ and got:

PS C:\ytdlp> python.exe -m pip install --force-reinstall socket

ERROR: Could not find a version that satisfies the requirement socket (from versions: none)
[notice] A new release of pip is available: 24.2 → 24.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
ERROR: No matching distribution

‘no matching distribution’ is the crux of my problems? what’s it mean? what to do?

use this instead:

PS C:\ytdlp> py substackgetvid.py

You don’t need the .exe. You also don’t need the entire word python. You can just use py.

However, first make sure that the module substackgetvid.py is located in C:\ytdlp. Or change the directory to where it is located.

well thanks for that but it’s just a shorter better way of issuing the command. it don’t do nothing to change the results of it: can’t install socket.

Well, socket and _socket come pre-installed with Python. Can you try re-installing Python?

Yep, as I said, I just did that. Have 3.13 now. Actually I have done it more than once this morning.
It is a dll load fail, it says. so i made sure scripts and the exe are all on user and system paths.
why is it trying to load it if it is core?

Try this.

  1. Start python as python -v
  2. At the >>> prompt type import socket

Python should list where it is trying to import socket and _socket from. Where does it try to load _socket from?

Can you find _socket file in your python install?

1 Like

Can you open IDLE and run your script from there? Are the results the same or does it work when launched from there?

Only just found your replies. sorry for the delay. answer second question first; i find these _socket files:
C:\Users\arthur\AppData\Local\Programs\Python\Python313\DLLs_socket.pyd
C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\test\test_socketserver.py
C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\test\test_socket.py
C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\test\mock_socket.py

for the socket search I get this:

>>> import socket
# C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\__pycache__\socket.cpython-313.pyc matches C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\socket.py
# code object from 'C:\\Users\\arthur\\AppData\\Local\\Programs\\Python\\Python313\\Lib\\__pycache__\\socket.cpython-313.pyc'

unfortunately none of it makes much sense to me. :frowning:

Any help if I publish the bit of code I am trying to run? I suppose not because it’s apparently a python install problem but if you like I can/will.

I ran IDE and ran the code from there.

and got this:

python
Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    python
NameError: name 'python' is not defined

====================== RESTART: C:\ytdlp\substackgetvid.py =====================
Traceback (most recent call last):
  File "C:\ytdlp\substackgetvid.py", line 3, in <module>
    import requests
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\requests\__init__.py", line 48, in <module>
    from charset_normalizer import __version__ as charset_normalizer_version
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\charset_normalizer\__init__.py", line 24, in <module>
    from .api import from_bytes, from_fp, from_path, is_binary
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\charset_normalizer\api.py", line 5, in <module>
    from .cd import (
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\charset_normalizer\cd.py", line 14, in <module>
    from .md import is_suspiciously_successive_range
AttributeError: partially initialized module 'charset_normalizer' from 'C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\charset_normalizer\__init__.py' has no attribute 'md__mypyc' (most likely due to a circular import)

Please, do not post screen shots of text. Use the pre-formatted text button </>.

My python is installed for “all users” in c:\python3.13.win63 unlike your user install so the paths are not the same.
Here is what I see on my Windows 11.

>>> import socket
# C:\python313.win64\Lib\__pycache__\socket.cpython-313.pyc matches C:\python313.win64\Lib\socket.py
# code object from 'C:\\python313.win64\\Lib\\__pycache__\\socket.cpython-313.pyc'
# extension module '_socket' loaded from 'C:\\python313.win64\\DLLs\\_socket.pyd'
# extension module '_socket' executed from 'C:\\python313.win64\\DLLs\\_socket.pyd'
import '_socket' # <_frozen_importlib_external.ExtensionFileLoader object at 0x000001B5325B3490>
# C:\python313.win64\Lib\__pycache__\selectors.cpython-313.pyc matches C:\python313.win64\Lib\selectors.py
# code object from 'C:\\python313.win64\\Lib\\__pycache__\\selectors.cpython-313.pyc'
import 'math' # <class '_frozen_importlib.BuiltinImporter'>
import 'selectors' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B532A30590>
import 'socket' # <_frozen_importlib_external.SourceFileLoader object at 0x000001B5327DE2D0>
>>>

In the past there have been a number of reasons for this type of problem.

  1. you have a user created socker.py file, rename it to something else.
  2. Your installation of python was damaged. Try to uninstall completely and then install again.
  3. Anti-virus or software from your employer is breaking python.
1 Like

It’s telling you where it is looking as it looks for socket and the things it depends on. The bit you post looks reasonable, but was it followed by a failure to find the DLL, or did it succeed in importing socket?

In the display Barry provides, you can see that CPython next finds a .pyd file in the DLL directory.

It is definitely the right approach to see if the basic import works without your application. If it fails, there’s something wrong with your installation/platform. Re-install, don’t pip install anything else yet, and see if you can track down how it breaks.

If it succeeds with the bare import already, but fails in your application, then there is something about the application, or where you run it from, that is interfering with this process. Try to creep up on the problem in that contex, maybe without requests e.g. if you put import socket at the top of your application file, does it fail there, or is it only inside requests that it fails?

A useful diagnostic is sys.path.

>>> import sys
>>> sys.path

It tells you where Python looks for things. Maybe something has changed it, or changes it as your application runs.

This error is different from the one you first posted. The first relates to a DLL, stemming from import urllib3 at requests\__init__.py, line 43 . The second suggests requests actually got past importing socket, since it fails at requests\__init__.py", line 48.

The second looks like requests is not installed correctly.

Comparing the exceptions raised between when running your script from the CMD line and IDLE, there is a difference between the imports that are being mentioned in the traceback as I have highlighted here :

# From your initial post when running from the CMD line
import requests
import urllib3
import socket
import _socket
from . import exceptions
# From running in IDLE
import requests
from charset_normalizer import __version__ as charset_normalizer_version
from .api import from_bytes, from_fp, from_path, is_binary
from .cd import (
from .md import is_suspiciously_successive_range

Exception raised:
AttributeError: partially initialized module

 # Warning given
(most likely due to a circular import)

As you can see, the exceptions raised are a bit different. When attempting to run your script from IDLE, you don’t get either an import socket exception or import _socket exception.

Looking up the warning for (most likely due to a circular import) that was given in the IDLE attempted run, it states the following:

In Python, a circular import occurs when two or more modules depend on each other, creating a loop in the import chain. This leads to a situation where a module tries to import another module that is still in the process of being initialized, causing an error.

Can you check your code to verify if this is occurring or not. If it is, you need to rectify it.
I recommend importing one module at a time to see when in fact you start experiencing an issue or when an exception starts being raised.

You also have this attempted relative import highlighted in the traceback:

from .cd import (  # what is '(', is this a typo or even valid?

Recheck all of your imports

Presumably that’s the first line of a multi-line statement. There’ll be a list of identifiers, and finally a close parens.

Thanks for all that.

Seems to me the next thing I have to do is another uninstall and reinstall.

You say don’t post screen shots of text. Use pre format. I thought I read instructions to use preformat for code ? and that’s what I’ve tried to do. though there is no code really, only output from it. I post no screenshots. that’s an image, ‘screenshots’. I cut and paste is all. If there’s a problem with my posts in this regard it looks like I need a little more ‘instruction’ for I’m not at all clear.

The errors have differed between postings probably because of the ‘requests’ thing.
The code fails and complains it is because of ‘requests’.
Hence I try to import requests.
And then I get errors on that which quote ‘socket’ as the problem.
So then I try to import ‘socket’ and get errors on that.
So three different commands giving three different errors.
I’m following suggestions because it is all beyond me.

I will post the source code, It is not lengthy. Then we will all know everything.

import re

import requests


def get_url(source_url, video_index):
    response = requests.get(source_url).content.decode()
    matches = re.findall('data-component-name="VideoEmbedPlayer" id="media-([^"]+)"', response)

    current_index = 0
    if video_index > 0:
        current_index = video_index - 1
        matches = [matches[current_index]]

    video_urls = []
    for media_id in matches:
        current_index += 1
        response = requests.get(
            f'https://simplicius76.substack.com/api/v1/video/upload/{media_id}/src',
            allow_redirects=False
        )

        video_urls.append((current_index, response.headers["Location"]))
    return video_urls


urls = get_url(
    source_url="https://simplicius76.substack.com/p/thunderbolt-from-the-skies-putins?utm_source=post-email-title&publication_id=1351274&post_id=151944706&utm_campaign=email-post-title&isFreemail=true&r=2ld2be&triedRedirect=true&utm_medium=email",
    video_index=1
)
for i, u in urls:
    print(f"Video index: {i} // Video url: {u}\n")
    
    

I will uninstall the whole thing. Do a search of files and do a search of registry. Remove all of python that i can. Then I will do a redownload, reinstall of this latest version of Python and see how I go then.

If there’s anything better I can do please let me know. :wink:

Just an fyi …

I noticed that this script that you uploaded does not have self-test code. Can you add this to your modules so that they are tested in isolation? This can help you verify that the modules that you are creating work as expected prior to being imported as a namespace to other modules.

Here is a very basic example:

def multi(x, y):

    return x * y

def addi(t, u):

    return t + u

# Verify that both functions work in isolation prior to importing to other
# modules.  This is only for script verification purposes to ensure objects 
# are running per requirements/expectations.
if __name__ == "__main__":  # Anything below this line is not imported to other modules

    print(f'The sum of 25 and 75 is: {addi(25, 75)} ')
    print(f'The product of 25 and 75 is: {multi(25, 75)}')

If there are importing issues, you might be able to catch them during self-tests which can make the debugging much easier.

Well I did that. uninstalled. uninstalled ‘python launcher’, too, which I found there, for good measure.

then just now downloaded 3.13 and installed it and ran command ‘python’:

Python 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
warning: can't use pyrepl: DLL load failed while importing unicodedata: The specified module could not be found.

doesn’t look good to me.
but anyway ran the script:

PS C:\ytdlp> py substackgetvid.py
Traceback (most recent call last):
  File "C:\ytdlp\substackgetvid.py", line 3, in <module>
    import requests
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\requests\__init__.py", line 43, in <module>
    import urllib3
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\__init__.py", line 14, in <module>
    from . import exceptions
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\exceptions.py", line 3, in <module>
    import socket
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\socket.py", line 52, in <module>
    import _socket
ImportError: DLL load failed while importing _socket: The specified module could not be found.
PS C:\ytdlp>

__version__” is not a valid library from the charset_normalizer package module according to this:

https://charset-normalizer.readthedocs.io/en/latest/api.html
and
https://charset-normalizer.readthedocs.io/en/latest/

Additionally, this exception:

has no attribute 'md__mypyc'

Is addressed here, providing a potential remedy:

PS C:\ytdlp> pip install -U --force-reinstall charset-normalizer
Collecting charset-normalizer
  Using cached charset_normalizer-3.4.0-cp313-cp313-win_amd64.whl.metadata (34 kB)
Using cached charset_normalizer-3.4.0-cp313-cp313-win_amd64.whl (102 kB)
Installing collected packages: charset-normalizer
  Attempting uninstall: charset-normalizer
    Found existing installation: charset-normalizer 3.4.0
    Uninstalling charset-normalizer-3.4.0:
      Successfully uninstalled charset-normalizer-3.4.0
Successfully installed charset-normalizer-3.4.0
PS C:\ytdlp> py substackgetvid.py
Traceback (most recent call last):
  File "C:\ytdlp\substackgetvid.py", line 3, in <module>
    import requests
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\requests\__init__.py", line 43, in <module>
    import urllib3
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\__init__.py", line 14, in <module>
    from . import exceptions
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\site-packages\urllib3\exceptions.py", line 3, in <module>
    import socket
  File "C:\Users\arthur\AppData\Local\Programs\Python\Python313\Lib\socket.py", line 52, in <module>
    import _socket
ImportError: DLL load failed while importing _socket: The specified module could not be found.

Am I looking at a broken Python installation or at bad code? I’m assuming broken install. But how so? It is new. Perhaps I should try a different version? Or even32 bit?

You’re forcing installation of random packages. I’m pretty sure something in there is broken as a result. Start clean, and only install things you actually need. Also, don’t trust ChatGPT when it tells you to do things.