Say, you work on a Python project from somebody else who used Linux (e.g., a Django or Flask project) and this project imports the
fcntl module from the Python Standard Library. 🐍
When you run the project on Linux everything works fine but if you run the project on Windows, it raises an
ModuleNotFoundError: No Module Named 'fcntl' because
fcntl is not available on Windows.
👉 Recommended Tutorial: ModuleNotFoundError: No Module Named ‘fcntl’ (Python)
💬 Question: Is it possible to modify the program so that you can still run it on Windows? In other words, is there a replacement for
fcntl on Windows?
fcntl module is an interface to the
ioctl() Unix routines for file and I/O control on file descriptors. It is super specific for the Unix OS API.
Theoretically, a replacement for
fcntl on windows would be
win32api calls. However, you’d use them differently and you cannot just import the Windows equivalent in Pyhton and fix the error messages or anything.
If things just would be that simple!
The sophisticated migration approach would analyze all different calls to the fcntl module and find a win32api call equivalent. If it doesn’t exist, you need to find a workaround. You may even find ways to skip the system calls because you may not even need to change file descriptors. In fact, in general it is not possible because some fcntl calls simply have no Windows equivalent.
But in most cases, going through the exercise and finding all usages of
fcntl calls and doing some thinking to replace or remove them will do the trick.
For example, say you used the
fcntl.lockf() function to do some file locking. You can use Windows functionality to accomplish the same thing without
fcntl (e.g., see here). Rinse. Repeat.
Simple but not easy.
I found a nice workaround to a subset of these problems here.
The idea is to create a custom
fcntl module by placing a file called
fcntl.py in your local project that Python can import so the
ModuleNotFoundError goes away. Now, you add all functions your code needs to the module so everything goes through without error. If the functions are not really needed, you can create dummy functions that return dummy values or
Here’s an example that replaces the four functions
def fcntl(fd, op, arg=0): return 0 def ioctl(fd, op, arg=0, mutable_flag=True): return 0 if mutable_flag else '' def flock(fd, op): pass def lockf(fd, operation, length=0, start=0, whence=0): pass
Of course, you can replace and modify these functions to your own needs, maybe even replace the functionality with Windows functionality. Or skip some functions if you don’t call them in the code. Or add any functions your code calls but that are not defined here.
⭐ This option is best if you just need to get it running but you cannot (or do not want to) change any line of code in the original project.
Alternative Library ‘waitress’
A Windows alternative to some usages of the Linux
fcntl package is the
waitress package that you can install using
pip install waitress.
pip install waitress
Waitress is a production-quality pure-Python WSGI server with very acceptable performance. It has no dependencies except ones which live in the Python standard library. It runs on CPython on Unix and Windows under Python 3.7+. (PyPI)
Learn more here.
Alternative Package portalocker
It has been pointed out at various places online that you can use the
portalocker module instead of
portalocker works for Unix and Windows API calls — it’s a cross-platform API for flock-style file locking in Python that maps
win32 API calls.
“Portalocker is a library to provide an easy API to file locking.”
fcntl is a bit more powerful so it’s not a perfect solution either.
portalocker, simply run:
pip install portalocker
portalocker instead of
fcntl and replace the code according to the documentation. Here’s an example use:
import portalocker lock = portalocker.RedisLock('my_lock_channel_name') with lock: print('do something')
Thanks for reading the whole tutorial! If you want to keep improving your Pyhton skills, feel free to download our Python cheat sheets here:
References used in this article:
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.