ModuleNotFoundError: No Module Named ‘fcntl’ (Python)

Did you get the ModuleNotFoundError: No Module Named 'fcntl'? This short tutorial will show you some useful ways that can help you resolve it.

The minimal occurrence of the error is when using Windows, for example, and you run import fcntl or any derivative of the import statement such as from fcntl import X but the fcntl package is not available on the operating system you’re trying this (e.g., Windows).

>>> import fcntl
Traceback (most recent call last):
  File "<pyshell#2>", line 1, in <module>
    import fcntl
ModuleNotFoundError: No module named 'fcntl'

It doesn’t help to run pip install fcntl either because there is no fcntl package for Windows. And on Linux fcntl is already part of the Python Standard Library!

PS C:\Users\xcent> pip install fcntl
ERROR: Could not find a version that satisfies the requirement fcntl (from versions: none)
ERROR: No matching distribution found for fcntl

The reason this doesn’t work on Windows is that “this module performs file control and I/O control on file descriptors. It is an interface to the fcntl() and ioctl() Unix routines.” (docs)

And it doesn’t really make sense to find an fcntl replacement for Windows (although, technically, there are some things you can do even though they are NOT very clean). Learn more here if you want to go down that route:

πŸ‘‰ Recommended Tutorial: Is There an fcntl Replacement on Windows?

Clearly, fcntl can only work on Unix-based systems!

On those Unix-based systems (e.g., Linux) where fcntl is available however you don’t even need to install it because it’s part of the Python Standard Library as you can see on this screenshot from the official docs:

Let’s assume you are on a Unix-based system (e.g., Linux), and you have a working Python installation, so fctnl should be available. Can the error still occur?

Yes, it can (although it’s very unlikely). By far the most frequent reason in tha case is that your Python installation is corrupted and you’d be well-advised to install or reinstall Python again.

🌍 Recommended Tutorial: How to Check ‘fcntl‘ Package Version in Python?