I’m assuming you mean this package. Taking a quick look, it hasn’t been maintained for well over decade, and the linked Bitbucket source repo is gone (I’m guessing it was a hg
repo deleted as part of Bitbucket dropping Mercurial support years ago).
The packaging uses (unsurprisingly, given its age) a bunch of deprecated stuff and crazy hacks that probably won’t work on any modern-ish Python, and certainly won’t soon, and the setup.py
doesn’t contain a python_requires
argument (unsurprisingly, given that metadata didn’t even exist yet at the time it was packaged) but does contain a manual install-time check for Python 2.6 or later, presumably including 2.7.
The latest version of Jinja, yes (Jinja 3), but if j2
either upper-capped its Jinja version, or used the python_requires
argument to make clear it required Python 2, pip
should automatically pull a compatible version.
You could manually install older compatible versions of dependencies on Python 3.6, but that requires that j2
work on Python 3.6, which is not impossible (you may actually be very lucky in this particular case), but doubtful for a >decade old package. More importantly, the packaging weirdness is likely to cause issues, and you’re very likely to run into incompatibilities with a bunch of old stuff dropped in the latest Jinja 3.x releases. What you really need to constrain is the Jinja version to at least <3, along with the updates discussed above if you want it to work with Python 3 and semi-modern packaging (which you should).
Wait, what? You just mentioned MarkupSafe 2.0.1 requires Python 3.6, so why would you expect it to work installing on Python 2.7? Also, your Python 2 pip
version is so old that it apparently doesn’t even realize that it installed an incompatible package.
To note, you should be using a venv
/virtualenv
environment so you don’t mess up your system Python installation, and possibly your OS itself, or run into other incompatibilities there, as well as upgrade (python -m pip install --upgrade pip
) to a much less ancient pip
version than whatever is bundled with your legacy OS.
Porting it shouldn’t actually be that hard; see below.
Every Python install on your machine is separate, so regardless of version, different Python installations (should) never see each others’ packages.
Most of the code is mostly just a CLI wrapper over Jinja with some helper functionality and extra variables injected, and isn’t that complex. Skimming through it, it looked fairly modern for its time, and I didn’t see that much that wouldn’t work unchanged on even the latest version of Python, aside from a few relatively minor and fixable potential trouble spots.
Packaging-wise it needs a little more work, but that shouldn’t be too tough either. While I suggest following the tutorial of the Packaging User Guide, the main must-do items are:
- Rename
j2
→ j2.py
to make it a proper Python module
- Stick the argument parsing and execution code blocks into a
main()
function
- In the
setup.py
, remove all the hacky code besides the actual setup()
call inside the main()
function, get rid of all the imports and replace them with from setuptools import setup
, and in the setup_args
dict, replace the scripts
line with entry_points = {'console_scripts': ['j2=j2:main']}
.
- Also in the
setup_args
dict, get rid of platform=...
, and 'argparse'
in the install_requires
(its in the stdlib now), constrain your version of Jinja appropriately (e.g. "<3"
) and add a line e.g. python_requires = ...
with your minimum required Python version, e.g. “>=3.6”` is a reasonable default for your use case.
There are other things you should do, see the packaging guide for that, but those are the minimum to get it working with semi-modern packaging tooling.
The biggest challenge is likely getting to work with recent versions of Jinja (especially Jinja >=3, which broke backward compat), but you should be able to get away with restricting it to Jinja <3 for now, or perhaps an earlier version.
This is because, as discussed above, you need to constrain the version of jinja2
(which is the direct dependency of the package) to at least <3
, and possibly lower, which is what requires MarkupSafe >=2.
So if you install jinja2==2.*
first or at the same time, (modern) pip
should at least solve correctly, though you might still run into one of other problem I’ve identified aren’t also an issue), and you might need to try an older Jinja if you run into problems at runtime. That said, this won’t work forever, since later versions of your OS might not include, or even be able to install Python 2, since it is EoL and obselete.