DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13

Greetings.

I have a website using althttpd webserver (Althttpd: The Althttpd Webserver) which strives for simplicity, security, and low resource usage. The home page is a python script that is working beautifully as of today. It’s running Python 3.12.9 (main, Feb 13 2025, 09:16:23) [GCC UCRT 14.2.0 64 bit (AMD64)] on a MSYS2 UCRT64 environment on a Windows 10 Pro system.

A few months ago, I started to get this warning on my logs after every hit on my site:

“C:/msys64/home/webUser/www/default.website/index.html:4: DeprecationWarning: ‘cgi’ is deprecated and slated for removal in Python 3.13”

I started a search for find a replacement for cgi with something like it. I have been spending more time than I should and it’s getting to be annoying, because there is no clear and easy way to do this. I have been using python since 1998 and I have never had this type of experience previously. There are many hits, with multiple ideas, but none of the suggestions is as simple as to change a few lines of code. All involve creating a new approach. I don’t want to redo my whole setup. Perhaps someone here can provide a replacement, here is a question that perhaps will help lots of people. These are my lines of code that I would love to be able to replace:

h = SetHTMLPage(h2,"")               # Line 1 has the html form
print(h)                             # Line 2 sends the html to the webserver
sform = cgi.FieldStorage()           # Line 3 creates the cgi form method
searchStr = sform.getvalue('search') # Line 4 gets the Searching input
rpt = sform.getvalue('Reports')      # Line 5 gets the Report input

I need replacements for line 3, 4 and 5. Any help would be greatly appreciated.

jic

What’s wrong with legacy-cgi?

There are many hits, with multiple ideas, but none of the suggestions is as simple as to change a few lines of code. All involve creating a new approach. I don’t want to redo my whole setup.

There are indeed a lot of different ways to solve the problem if you’re open to new approaches (including the option of installing a third-party package of the old module which is “simple” if you’re already installing other packages and requires no changes to your code at all to use it, but if you’re not yet using anything outside the stdlib and this is the first time you’ve needed to use packaged modules it might require learning new tools).

Don’t forget, though, that updating to new versions of Python will sometimes require making (even significant) changes to your own scripts, as well as learning to do new things you haven’t before. The only way to avoid that is to not upgrade.

Requirement already satisfied: legacy-cgi in c:/msys64/ucrt64/lib/python3.12/site-packages (2.6.2)

That is the first thing I did a few weeks ago.

Requirement already satisfied: legacy-cgi in c:/msys64/ucrt64/lib/python3.12/site-packages (2.6.2)

But, I still get those warnings. If those warnings will be there after v3.13, and it continues to work, I can live with that. I can clean those from the log.

Provide one, my friend. Give me the minimal import, lines, to cover those lines, and I will work with it. I may choose not, depending on how much heavier the library will be. All I would like is to have someone say, here is the answer: “pip install XLib, then… then… replace line 3 with this 4 lines, etc.”

Installed packages have lower position in sys.path than built-in modules. Therefore, import cgi will continue to import Python 3.12’s built-in module, which triggers the warning. When you upgrade to Python 3.13, the built-in module will no longer exist, and import cgi will see the module provided by legacy-cgi. Until then, you can ignore the specific warning:

import warnings
warnings.filterwarnings("ignore", "'cgi' is deprecated", DeprecationWarning)
import cgi
# no warning output

I created an issue on legacy-cgi to discuss this: Document that this is not imported on Python < 3.13 · Issue #10 · jackrosenthal/legacy-cgi · GitHub

1 Like

Provide one, my friend. Give me the minimal import, lines, to cover those lines, and I will work with it. I may choose not, depending on how much heavier the library will be. All I would like is to have someone say, here is the answer: “pip install XLib, then… then… replace line 3 with this 4 lines, etc.”

You stated you’d already seen the suggestions, they just weren’t simple enough for your liking. The one I was referring to is:

pip install legacy-cgi

(With no changes needed to your script.)

But that was already mentioned anyway, and it sounds like you’ve also installed it even though you don’t need it before 3.13. It won’t silence the deprecation warnings because you’re still using the version from stdlib as it continues to exist for the interpreter release you’re on today.

If you’re just looking for a way to silence deprecation warnings, see warnings — Warning control — Python 3.13.2 documentation for how to do so. You can filter them by pattern match so that you only silence ones you don’t care about.

This is working beautifully. Thanks David. If what you said is true, I am all set. Thanks.

1 Like

I apologize for the explosive replied. I was not meant to be. I saw the warnings, even after I install the legacy-cgi, and I said this is not work. I should have created a different post regarding warnings even after installing legacy-cgi. So, please forgive this poor soul. Thanks guys. All is well.

1 Like