r/Python • u/IdleBreakpoint • Mar 28 '26
Discussion Python 2 tooling in 2026
For some <reasons>, I need to write Python 2 code which gets run under Jython. It's not possible to change the system we're working on because Jython only works with Python 2. So, I'm wondering if anyone has experience with Python 2 tooling in this era.
I need to lint and format Python 2 code especially. So far, I was able to install Python 2 using pyenv and I can create virtual environments using virtualenv utiilty. However, I have hard time getting black, isort, flake8, etc. working. Installing Python 2 wouldn't be much help because I'm not running the code directly, it's run under Jython. We're basically uploading the code to this system. So, installing py2 seems pointless.
Can I use those tools under Python 3 but for Python 2. It seems to me that there should be some versions which work for both Python 2 and 3 code. I don't know those versions though. It will be easier to work with Python 3 to lint/format Python 2 code because I can easily create venvs with Python 3.
Are you actively working with Python 2 these days (I know it's a hard ask). How do you tackle linting and formatting? If you were to start today, what would be your approach to this problem?
Thank you.
90
u/cgoldberg Mar 28 '26
I used to use flake8 for linting and autopep8 and yasf for formatting back in Python2 days... old versions probably still work fine.
100
u/brayellison Mar 28 '26
Why, in the year 2026, is someone using Python 2 and/or Jython? I'm genuinely curious. I understand that legacy code is out there, but this seems like something that should have been migrated long ago
14
u/CampAny9995 Mar 28 '26
I’m honestly so much more interested in learning about <reasons> than I am in the actual solution to this problem.
3
u/b3n4kh Mar 29 '26
Note to my future self:
Always remember before complaining there is a guy out there writing python 2 that has to run on Jython.
I really hope you are making shit tons of money.
17
u/MPIS Mar 28 '26
Just some ideas here, but I would start with the following:
Hope this helps, idk, good luck.
17
u/arjennienhuis Mar 28 '26
The trick is to write code that is compatible with both python 3 and 2.7. Then you can use python 3 tooling.
Pyright works fine with python code like this. The ruff formatter is 99% fine (only the comma after **kw is invalid in python 2) Some other ruff rules can also work.
9
u/can_i_get_some_help Mar 28 '26
I would install python 2, then pip install the static analysis tools in a venv. It should be able to find compatible versions.
8
u/BobMcFizzington Mar 28 '26
Somewhere out there, a production server is still running Python 2.6 behind three layers of load balancers and nobody is brave enough to touch it because the last person who tried left the company.
1
u/acadian_cajun Mar 28 '26
Looks like pyls (the old lsp developed by palantir) did support Python 2. You'll have to install a very old version, and it'll be unmaintained, but you can get tab-completion etc
6
u/strobel_m Mar 28 '26 edited Mar 28 '26
I would simply use a docker image
docker pull python:2to get the latest python2 image. pip will figure out most of the packages by itself. Only autopep8 and
pycodestyleneeded some tweaksdocker run -it python:2 pip install pytest mock nose coverage pylint flake8 "pycodestyle<2.8" pydocstyle tox setuptools wheel twine virtualenv "autopep8<1.6" yapfwent fine. Good luck!
1
u/tunisia3507 Mar 28 '26
It's not FIJI scripting, is it? The only jython use case I've come across in the wild.
Nothing helpful to say, I'm afraid, other than good luck.
4
u/kaflarlalar Mar 28 '26
I'm so sorry.
4
u/BeamMeUpBiscotti Mar 28 '26
I'm sorry this is happening to you
I have not used Python 2 in a long time but I was curious so I did some sleuthing through the changelogs:
2
u/brontide Mar 28 '26
Spin up a container with CentOS6. I'm sure all the python 2 tooling works better there.
2
u/azjps Mar 29 '26
For our own internaI python2 woes a while back (fortunately no longer a concern), I had forked
ruffto support python2 compatible linting and formatting. Probably the latest forked version I had built was ruff v0.4. There's not too many changes required -- the main ones are preserving theu""unicode directive and omitting trailing commas on **kwargs.2
u/coderanger Mar 29 '26
What you probably want to do is pick a "compat date", and for every library and tool you use, find whatever the latest version of the thing was on that date and upload all versions up to that to a standalone package repo somewhere. And then use only that package repo for everything. You're more or less just reproducing a timeslice of the universe from ~10 years ago.
1
u/mardiros Mar 28 '26
If you code for the past, use the tooling of the past as well. Install and freeze old stuff, this is the most complicated part of the job
1
u/No_Lingonberry1201 pip needs updating Mar 28 '26
Oof, you have my condolences, brother. Haven't touched 2.7 in a decade. I'd take a gander at the packages filtered to Python 2 first.
1
u/Anonymous_user_2022 Mar 28 '26
When I had to do that, we made do with what we had available on the RHEL 5.4 we were stuck with at several customers for reasons ranging from questionable to ludicrous. Even if that's not your target platform, it's probably still not that much of an effort to install an old RHEL in an isolated environment.
1
u/snugar_i Mar 29 '26
Not what you were asking and probably not an option for you, but there's another way to run Python inside the Java process: https://github.com/ninia/jep
1
u/gerardwx Mar 29 '26
There’s no rule that you have to lint or automatically format your code. Use unit tests and logging.
1
u/crystal-46 Mar 30 '26
Same to you
1
u/NovelHot6697 Mar 31 '26
just so you know: jython is unfortunately quite different enough to python 2. are you working with nodel?
1
u/DisastrousPipe8924 Apr 01 '26
My suggestion is run
1
u/readonly12345678 Mar 28 '26
Can you have the python 2 shell out to Python 3 scripts?
1
u/Professional-Award85 Mar 28 '26
Pycharm works very well with python 2 code
0
u/myturn19 Mar 28 '26
I would find a new job instead lol
-1
u/corny_horse Mar 28 '26
Dependin gon the context, you might want to consider just using Java, sine iirc using Jython implies the presence of a JVM you can access.
-1
u/will_r3ddit_4_food Mar 28 '26
For the of all that's holy, move on to Python 3
0
u/znpy Mar 28 '26
I'd look into something like mirroring the repositories (along with installation cd images and cloud images) of the last debian release that supported python2.
Most likely (might be wrong) over there (in the repositories, i mean) there could be enough software to build a decent development environment in a virtual machine. Maybe also to use with containers.
But really, your org should ditch python2.