# Unknown encoding: idna in socket.py

**URL:** <https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380>\
**Category:** Python Help\
**Created:** [November 30, 2026, 1:18pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380 "2026-11-30T13:18:45Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [November 30, 2026, 1:18pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/1 "2026-11-30T13:18:45Z")

</div>

Encountered a problem (possible bug?) in `socket.py` related to idna encoding:

However this only happens in runtime mode, it works fine in debug mode!

Error:

```python
  File "C:\Program Files\Python315\Lib\socket.py", line 826, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
                                ~~~~~~~~~~~~~ ^^^^^^
LookupError: unknown encoding: idna

```

Main.py:

```python
from wsgiref import simple_server
import Import1

def dummy():
    pass
httpd = simple_server.make_server("", 8000, dummy)

```

Import1.py:

```python
import ImportHook

```

ImportHook.py:

```python
import sys
from importlib.machinery import SourceFileLoader, FileFinder

class PreProcessLoader(SourceFileLoader): # SourceLoader
    def source_to_code(self, data, path, fullname, *, _optimize: int = -1):
        return super().source_to_code(data, path, fullname, _optimize=_optimize)

sys.path_hooks.insert(0, FileFinder.path_hook( (PreProcessLoader, [".py"]) ))
sys.path_importer_cache.clear()

```

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [November 30, 2026, 11:52pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/2 "2026-11-30T23:52:24Z")

</div>

I think it has something to do with you clearing the import cache. Try `import encodings.idna` before `from wsgiref import simple_server`.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 1, 2026, 9:20pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/3 "2026-12-01T21:20:21Z")

</div>

@jonathandung That works, thanks.

However it also works if I put the same `import encodings.idna` between `sys.path_hooks.insert(0...` and `sys.path_importer_cache.clear()`. That suggests it may not be so much to do with the `cache.clear()` line as the `sys.path_hooks.insert(0...`.

I have tried `print(sys.modules)` both before and after the `cache.clear` but can’t see any obvious differences.

Although this is a good quick fix for now, it would be preferable to get to the root cause so that it doesn’t come back to bite us in the future sometime.

---

<div class="post-metadata">

**Author:** ![mgedmin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mgedmin/32/2190_2.png) [@mgedmin](https://discuss.python.org/u/mgedmin)\
**Post date:** [December 2, 2026, 9:39am UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/4 "2026-12-02T09:39:31Z")

</div>

I’m unfamiliar with the details of the Python import machinery, but this looks like an interesting problem.

Can you import any other .py file after installing your hook into sys.path\_hooks?

Shouldn’t the source\_to\_code() of your SourceFileLoader subclass allow the fullname argument to be omitted (with a default value of `None`)?

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 2, 2026, 1:48pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/5 "2026-12-02T13:48:43Z")

</div>

@mgedmin Yes other files can be imported, the error does not show up until it hits the `httpd = simple_server.make_server("", 8000, dummy)` line.

You are quite right the fullname argument can, and perhaps should, be omitted, to the upstream call thanks for spotting that. I seem to recall this argument was new in 3.15 and I had to add it in to get the import hook working; and then to stop the type annotation warnings.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 3, 2026, 5:40pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/6 "2026-12-03T17:40:26Z")

</div>

A bit more info: if I move `import encodings.idna` to before the `httpd = simple_server.make_server` line I then get error:  
`ModuleNotFoundError: Standard library module 'unicodedata' was not found`

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [December 3, 2026, 6:21pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/7 "2026-12-03T18:21:46Z")

</div>

> [@NickC](#):
>
> ModuleNotFoundError: Standard library module ‘unicodedata’ was not found

So something in your import hook is breaking standard library imports. The exact failing module is likely just coincidence.

See if you can figure out exactly what your loader is getting asked for when you import a stdlib module. My guess is you need to add something that says “oh this one’s not for me, use the default handling”.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 4, 2026, 12:12am UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/8 "2026-12-04T00:12:30Z")

</div>

@Rosuav I have now added this `print` into my `source _to_code`:

```python
class PreProcessLoader(SourceFileLoader): # SourceLoader
    def source_to_code(self,
                       data: Buffer | str | ast.Module | ast.Expression | ast.Interactive,
                       path: bytes | str | os.PathLike[str],
                       fullname: str | None = None, *, _optimize: int = -1) -> CodeType:
        print("In source_to_code:", path)
        return super().source_to_code(pre_process(data, os.fsdecode(path)), path, _optimize=_optimize)

```

Of course that did nothing because the standard library imports didn’t need recompiling, so I then deleted (renamed) `C:\Program Files\Python315\Lib\ __pycache__ ` which caused permission problems so I created a new `C:\Program Files\Python315\Lib\ __pycache__ ` with full permissions for all users.

This now runs and I can see it importing various standard library modules but unfortunately still get the same `LookupError: unknown encoding: idna`.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [December 4, 2026, 12:20am UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/9 "2026-12-04T00:20:05Z")

</div>

You’re subclassing `SourceFileLoader`, so you get [all of its existing methods](https://docs.python.org/3/library/importlib.html#importlib.machinery.SourceFileLoader). I’d start by overriding each of the [methods of SourceLoader](https://docs.python.org/3/library/importlib.html#importlib.abc.SourceLoader) with one that prints out status and then returns super, as it’s entirely possible your `PreProcessLoader` is doing something it doesn’t need to.

As a side point: In digging through the docs to find where `source_to_code` is called, I find that it’s meant to be a static method. Is that possibly relevant?

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 4, 2026, 12:48am UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/10 "2026-12-04T00:48:07Z")

</div>

Have now removed `pre_process()` from the equation completely. `source_to_code` is now:

```python
class PreProcessLoader(SourceFileLoader): # SourceLoader
    def source_to_code(self,
                       data: Buffer | str | ast.Module | ast.Expression | ast.Interactive,
                       path: bytes | str | os.PathLike[str],
                       fullname: str | None = None, *, _optimize: int = -1) -> CodeType:
        print("In source_to_code:", path)
        return super().source_to_code(data, path, _optimize=_optimize)

```

Same idna error.

By the way also made it a `@staticmethod`but still no change.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 4, 2026, 12:51am UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/11 "2026-12-04T00:51:24Z")

</div>

One thing I have now found is that:

```python
import unicodedata
import encodings.idna
httpd = simple_server.make_server("", 8000, dummy)

```

causes:

```python
import unicodedata
ModuleNotFoundError: Standard library module 'unicodedata' was not found

```

I can’t seem to find `unicodedata` anywhere in my Python 3.15 installation?

---

<div class="post-metadata">

**Author:** ![mgedmin](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mgedmin/32/2190_2.png) [@mgedmin](https://discuss.python.org/u/mgedmin)\
**Post date:** [December 4, 2026, 8:20am UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/12 "2026-12-04T08:20:14Z")

</div>

It’s a builtin module, like `sys`.

```python
>>> import unicodedata
>>> unicodedata. __loader__
<class '_frozen_importlib.BuiltinImporter'>

```

---

<div class="post-metadata">

**Author:** ![jonathandung](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jonathandung/32/35459_2.png) [@jonathandung](https://discuss.python.org/u/jonathandung)\
**Post date:** [December 4, 2026, 10:06am UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/13 "2026-12-04T10:06:30Z")

</div>

How did you install Python?

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 4, 2026, 3:49pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/14 "2026-12-04T15:49:04Z")

</div>

@mgedmin  
so it seems my system can’t find `unicodedata` for some reason.

If I revert to just:

```python
import encodings.idna
httpd = simple_server.make_server("", 8000, dummy)

```

I get error:

```python
  File "C:\Program Files\Python315\Lib\encodings\idna.py", line 3, in <module>
    import stringprep, re, codecs
  File "C:\Program Files\Python315\Lib\stringprep.py", line 10, in <module>
    import unicodedata
ModuleNotFoundError: Standard library module 'unicodedata' was not found

```

Although that still doesn’t ex[plain why I need to `import encodings.idna` in the first place.

@jonathandung  
Python was installed from `python-3.15.2-amd64.exe`.

---

<div class="post-metadata">

**Author:** ![aroberge](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aroberge/32/6200_2.png) [@aroberge](https://discuss.python.org/u/aroberge)\
**Post date:** [December 4, 2026, 4:04pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/15 "2026-12-04T16:04:05Z")

</div>

> [@NickC](#):
>
> so it seems my system can’t find `unicodedata` for some reason.

Have you tried to use the REPL to import `unicodedata`? Something like this:

```python
Python 3.15.2 (tags/v3.15.2:5e58eaa, Oct 25 2026, 17:27:03) [MSC v.1951 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import unicodedata

```

It does not raise any error on my computer. If this works for you, then clearly it is something that is incorrect with the rest of your code. I suspect that it is your use of `sys.path_importer_cache.clear()` which likely removes the frozen module importer finder.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 4, 2026, 4:46pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/16 "2026-12-04T16:46:35Z")

</div>

from REPL

```python
>>> import sys
>>> sys.path_importer_cache.clear()
>>> import unicodedata

```

runs fine. Also running same from test prog runs fine.

The problem is definately the import hook for some unexplained reason:

```python
from importlib.machinery import SourceFileLoader, FileFinder

class PreProcessLoader(SourceFileLoader): # SourceLoader
    def source_to_code(self,
                       data,path,fullname = None, *, _optimize: int = -1):
        print("In source_to_code:", path)
        return super().source_to_code(data, path, _optimize=_optimize)

import sys
sys.path_hooks.insert(0, FileFinder.path_hook( (PreProcessLoader, []) ))
sys.path_importer_cache.clear()

import socket # <- fails: ModuleNotFoundError: No module named 'socket'

```

---

<div class="post-metadata">

**Author:** ![ronaldoussoren](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/ronaldoussoren/32/61_2.png) [@ronaldoussoren](https://discuss.python.org/u/ronaldoussoren)\
**Post date:** [December 4, 2026, 9:08pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/17 "2026-12-04T21:08:35Z")

</div>

You’re inserting a path hook that handles all filesystem directories. Such a path hook should AFAIK handle all kind of modules and in particular extension modules (which unicodedata is). The hook you’re inserting only handles “.py” source files.

See `_install` in `_bootstrap_external.py` for how the standard library creates the default path hooks for path-based path entries.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 5, 2026, 2:01pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/18 "2026-12-05T14:01:24Z")

</div>

@ronaldoussoren I may be misunderstanding how to use an import hook but, reverting to the example below

```python
from importlib.machinery import SourceFileLoader, FileFinder

@staticmethod
class PreProcessLoader(SourceFileLoader): # SourceLoader
    def source_to_code(data, path, fullname = None, * ,_optimize = -1):
        print("In source_to_code:", path)
        # return None
        return super().source_to_code(data, path, fullname, _optimize=_optimize)

import sys
sys.path_hooks.insert(0, FileFinder.path_hook( (PreProcessLoader, [".py"]) ))
sys.path_importer_cache.clear()
import unicodedata # <- fails: ModuleNotFoundError: Standard library module 'unicodedata' was not found

```

Isn’t the call to `super().source_to_code(data, path, _optimize=_optimize)` supposed to handle external modules? Either that or my PreProcessLoader tells Python that we have not handled this import and to move on to the next (FileFinder) loader in the `sys.path_hooks` list; I have tried returning None but that doesn’t help.

---

<div class="post-metadata">

**Author:** ![aroberge](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/aroberge/32/6200_2.png) [@aroberge](https://discuss.python.org/u/aroberge)\
**Post date:** [December 5, 2026, 3:09pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/19 "2026-12-05T15:09:12Z")

</div>

> [@NickC](#):
>
> ```python
> sys.path_importer_cache.clear()
> 
> ```

Why do you think you need this? By inserting your import hook at the beginning of `sys.path_hooks`, it will be used whenever possible. If you remove it, the exception will go away.

---

<div class="post-metadata">

**Author:** ![NickC](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/nickc/32/37293_2.png) [@NickC](https://discuss.python.org/u/NickC)\
**Post date:** [December 5, 2026, 3:33pm UTC](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380/20 "2026-12-05T15:33:09Z")

</div>

Without the `sys.path_importer_cache.clear()` the custom SourceFileLoader never runs.

[Next page](https://discuss.python.org/t/unknown-encoding-idna-in-socket-py/110380.md?page=2)
