Python Forum
How can set different Jinja2 version for different app?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can set different Jinja2 version for different app?
#1
Jinja2 is a templating engine,sphinx need it to work. My sphinx use jinja2 in /usr/lib/python3/dist-packages/jinja2 whose version is 2.11.3.I installed some new app with pip,a new jinja2 version installed in /home/debian/.local/lib/python3.9/site-packages/jinja2/ whose version is 3.1.2.
I found that the sphinx can't work when to make html!

make html
Traceback (most recent call last):
File "/usr/bin/sphinx-build", line 33, in <module>
sys.exit(load_entry_point('Sphinx==3.4.3', 'console_scripts', 'sphinx-build')())
File "/usr/bin/sphinx-build", line 25, in importlib_load_entry_point
return next(matches).load()
File "/usr/lib/python3.9/importlib/metadata.py", line 77, in load
module = import_module(match.group('module'))
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 790, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/usr/lib/python3/dist-packages/sphinx/cmd/build.py", line 25, in <module>
from sphinx.application import Sphinx
File "/usr/lib/python3/dist-packages/sphinx/application.py", line 43, in <module>
from sphinx.registry import SphinxComponentRegistry
File "/usr/lib/python3/dist-packages/sphinx/registry.py", line 24, in <module>
from sphinx.builders import Builder
File "/usr/lib/python3/dist-packages/sphinx/builders/__init__.py", line 26, in <module>
from sphinx.util import import_object, logging, progress_message, rst, status_iterator
File "/usr/lib/python3/dist-packages/sphinx/util/rst.py", line 21, in <module>
from jinja2 import Environment, environmentfilter
ImportError: cannot import name 'environmentfilter' from 'jinja2' (/home/debian/.local/lib/python3.9/site-packages/jinja2/__init__.py)
make: *** [Makefile:19: html] Error 1

I can remove new installed jinja2 /home/debian/.local/lib/python3.9/site-packages/jinja2 for sphinx,but the new app can't work,how can fix it?
Reply
#2
Use virtual enviroment.
Example install jinja2 version 2.11.3.
# Make 
tom@tom-VirtualBox:~$ python -m venv jin_env
# cd in
tom@tom-VirtualBox:~$ cd jin_env/
# Activate
tom@tom-VirtualBox:~/jin_env$ source bin/activate
# Install
(jin_env) tom@tom-VirtualBox:~/jin_env$ pip install Jinja2==2.11.3
Collecting Jinja2==2.11.3
  Downloading Jinja2-2.11.3-py2.py3-none-any.whl (125 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 125.7/125.7 KB 2.2 MB/s eta 0:00:00
Collecting MarkupSafe>=0.23
  Downloading MarkupSafe-2.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (25 kB)
Installing collected packages: MarkupSafe, Jinja2
Successfully installed Jinja2-2.11.3 MarkupSafe-2.1.1

(jin_env) tom@tom-VirtualBox:~/jin_env$ pip list
Package    Version
---------- -------
Jinja2     2.11.3
MarkupSafe 2.1.1
pip        22.0.4
setuptools 58.1.0
So now install Sphinx,if need a spesific version do that same with ==.
This now a isolated Python version,and what you have installed before dos not matter.

Also when it install to .local/lib/python3.9 is the same as pip install --user some_pkg which is fine.
It will install there if not have have root privileges to /usr/lib/python3/.
sudo pip install some_pkg will install to folder over,but should not use sudo to install.
ndc85430 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Jinja2 and Excel zinho 1 4,482 Mar-26-2021, 12:27 AM
Last Post: zinho
  Trying to use Jinja2 for loop with Python 3.6 benniehanas 0 1,255 Feb-12-2020, 02:44 PM
Last Post: benniehanas
  Can I upload a new version without previously deleting ancient version sylas 6 4,184 Nov-08-2017, 03:26 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020