5

I am working with a Python virtualenv named env to execute Odoo. In the virtualenv bin directory, I got this:

pip
pip3
pip3.8

python -> /usr/bin/python3
python3 -> python
python3.8 -> python

And the Odoo service is running this command to execute Odoo (as you can see, using the python3.8 of the virtualenv):

ExecStart=/opt/odoo/env/bin/python3.8 /opt/odoo/odoo_13/src/oca/OCB/odoo-bin -c /opt/odoo/odoo_13/config/.odoo.conf

The problem is that every now and again, Odoo gives the following critical error and the instance stops working:

ModuleNotFoundError: No module named 'werkzeug'

It seems that the werkzeug package is not installed, but, if I do the following:

anubia@my-server:/opt/odoo$ . env/bin/activate

(env) anubia@my-server:/opt/odoo$ which pip
/opt/odoo/env/bin/pip

(env) anubia@my-server:/opt/odoo$ which pip3
/opt/odoo/env/bin/pip3

(env) anubia@my-server:/opt/odoo$ which pip3.8
/opt/odoo/env/bin/pip3.8

(env) anubia@my-server:/opt/odoo$ pip3.8 install werkzeug
Requirement already satisfied: werkzeug in ./env/lib/python3.8/site-packages (0.11.15)

(env) anubia@my-server:/opt/odoo$ cd env/lib/python3.8/site-packages/
Display all 125 possibilities? (y or n)
Babel-2.9.1.dist-info/             attrs-21.2.0.dist-info/            docutils/                          numpy-1.21.0.dist-info/            python_stdnum-1.16.dist-info/      urllib3-1.26.6.dist-info/
Crypto/                            babel/                             docutils-0.17.1.dist-info/         numpy.libs/                        pytz/                              validate_email-1.3.dist-info/
Jinja2-3.0.1.dist-info/            bcrypt/                            gevent/                            paramiko/                          pytz-2021.1.dist-info/             vatnumber/
MarkupSafe-2.0.1.dist-info/        bcrypt-3.2.0.dist-info/            gevent-21.1.2.dist-info/           paramiko-2.7.2.dist-info/          reportlab/                         vatnumber-1.2.dist-info/
OpenSSL/                           cached_property-1.5.2.dist-info/   greenlet/                          passlib/                           reportlab-3.5.68.dist-info/        werkzeug/
PIL/                               cachetools/                        greenlet-1.1.0.dist-info/          passlib-1.7.4.dist-info/           reportlab.libs/                    wheel/
Pillow-8.3.1.dist-info/            cachetools-4.2.2.dist-info/        html2text/                         phonenumbers/                      requests/                          wheel-0.36.2.dist-info/
Pillow.libs/                       certifi/                           html2text-2020.1.16.dist-info/     phonenumbers-8.12.27.dist-info/    requests-2.25.1.dist-info/         xlrd/
PyNaCl-1.4.0.dist-info/            certifi-2021.5.30.dist-info/       idna/                              pip/                               requests_file-1.5.1.dist-info/     xlrd-2.0.1.dist-info/
PyPDF2/                            cffi/                              idna-2.10.dist-info/               pip-21.3.1.dist-info/              requests_toolbelt/                 xlsxwriter/
PyPDF2-1.26.0.dist-info/           cffi-1.14.5.dist-info/             isodate/                           pkg_resources/                     requests_toolbelt-0.9.1.dist-info/ xlwt/
PyYAML-3.12.dist-info/             cffi.libs/                         isodate-0.6.0.dist-info/           polib-1.1.1.dist-info/             sassutils/                         xlwt-1.3.0.dist-info/
Unidecode-1.2.0.dist-info/         chardet/                           jinja2/                            psutil/                            setuptools/                        xmlsig/
Werkzeug-0.11.15.dist-info/        chardet-4.0.0.dist-info/           libsass-0.21.0.dist-info/          psutil-5.8.0.dist-info/            setuptools-57.1.0.dist-info/       xmlsig-0.1.5.dist-info/
XlsxWriter-1.4.4.dist-info/        cryptography/                      lxml/                              psycopg2/                          six-1.16.0.dist-info/              yaml/
__pycache__/                       cryptography-3.4.7.dist-info/      lxml-4.6.3.dist-info/              psycopg2-2.9.1.dist-info/          stdnum/                            zeep/
_distutils_hack/                   dateutil/                          markupsafe/                        pyOpenSSL-20.0.1.dist-info/        suds/                              zeep-4.0.0.dist-info/
appdirs-1.4.4.dist-info/           decorator-5.0.9.dist-info/         nacl/                              pycparser/                         suds_jurko-0.6.dist-info/          zope/
asn1crypto/                        defusedxml/                        num2words/                         pycparser-2.20.dist-info/          tests/                             zope.event-4.5.0.dist-info/
asn1crypto-1.4.0.dist-info/        defusedxml-0.7.1.dist-info/        num2words-0.5.10.dist-info/        pycryptodome-3.10.1.dist-info/     unidecode/                         zope.interface-5.4.0.dist-info/
attr/                              docopt-0.6.2.dist-info/            numpy/                             python_dateutil-2.8.1.dist-info/   urllib3/

So werkzeug seems to be installed. What is the problem?

EDIT

After trying with @ChesuCR answer two weeks, the Odoo server stopped working again. This time the error is:

ModuleNotFoundError: No module named 'PyPDF2'

However, if I check the virtualenv:

myuser@myserver:/opt/odoo$ . venv3.7/bin/activate
(venv3.7) myuser@myserver:/opt/odoo$ which python3.7
/opt/odoo/venv3.7/bin/python3.7
(venv3.7) myuser@myserver:/opt/odoo$ python3.7 -m pip install PyPDF2
Requirement already satisfied: PyPDF2 in ./venv3.7/lib/python3.7/site-packages (1.26.0)
(venv3.7) myuser@myserver:/opt/odoo$ python3.7 -m pip list
Package           Version
----------------- ---------
appdirs           1.4.4
attrs             21.2.0
Babel             2.6.0
beautifulsoup4    4.10.0
cached-property   1.5.2
certifi           2021.10.8
cffi              1.15.0
chardet           3.0.4
cryptography      36.0.0
decorator         4.3.0
defusedxml        0.7.1
docutils          0.14
ebaysdk           2.1.5
gevent            1.5.0
greenlet          0.4.15
html2text         2018.1.9
idna              2.8
isodate           0.6.0
Jinja2            2.10.1
libsass           0.17.0
lxml              4.3.2
Mako              1.0.7
MarkupSafe        1.1.0
mock              2.0.0
num2words         0.5.6
ofxparse          0.19
passlib           1.7.1
pbr               5.8.0
Pillow            6.1.0
pip               21.3.1
polib             1.1.0
psutil            5.6.6
psycopg2          2.7.7
pyasn1            0.4.8
pyasn1-modules    0.2.8
pycparser         2.21
pydot             1.4.1
pyOpenSSL         21.0.0
pyparsing         2.2.0
PyPDF2            1.26.0
pyserial          3.4
python-dateutil   2.7.3
python-stdnum     1.17
pytz              2019.1
pyusb             1.0.2
qrcode            6.1
reportlab         3.5.13
requests          2.21.0
requests-toolbelt 0.9.1
setuptools        57.5.0
six               1.16.0
soupsieve         2.3.1
urllib3           1.24.3
vatnumber         1.2
vobject           0.9.6.1
Werkzeug          0.14.1
wheel             0.37.0
xlrd              1.1.0
XlsxWriter        1.1.2
xlwt              1.3.0
zeep              3.2.0

And of course Odoo is running with the Python interpreter of the virtualenv:

ExecStart=/opt/odoo/venv3.7/bin/python3.7 /opt/odoo/odoo_13/src/oca/OCB/odoo-bin -c /opt/odoo/odoo_13/config/.odoo.conf
forvas
  • 9,801
  • 7
  • 62
  • 158
  • 2
    It's weird for `python` inside a virtualenv to be just a symlink to `/usr/bin/python3`. Try creating a new virtualenv alongside this one. – tripleee Nov 29 '21 at 12:07
  • 1
    Is there maybe another python path defined at the top of `/opt/odoo/odoo_13/src/oca/OCB/odoo-bin`? Something like `#!/usr/bin/python`? According to https://github.com/odoo/odoo/blob/15.0/odoo-bin this might be the case. Maybe this is somehow conflicting. – rocksteady Nov 29 '21 at 12:13
  • @tripleee that's true, my old `virtualenvs` have their own Python command interpreter, but the new ones, regardless the server, point to the local command interpreter... I've tried to create another `virtualenv` but same result. – forvas Nov 29 '21 at 12:18
  • @rocksteady this is the case. The `odoo-bin` has the shebang `#!/usr/bin/env python3`. But does the shebang taken into account when the file is executed with a specific command interpreter, like in this case? – forvas Nov 29 '21 at 12:20
  • 1
    Indeed, the shebang is ignored if you run `python scriptname`; the trick then of course is to make sure `python` points to the right instance of Python. – tripleee Nov 29 '21 at 12:21
  • 1
    How are you creating these virtual environments? The standard command with Python 3 would be `python3 -m venv envname` – tripleee Nov 29 '21 at 12:22
  • I use `virtualenv -p python3 env` or just `virtualenv env` to create the virtuaenv. If I use your command I get an error: **The virtual environment was not created successfully because ensurepip is not available. On Debian/Ubuntu systems, you need to install the python3-venv package using the following command: apt install python3.8-venv. You may need to use sudo with that command. After installing the python3-venv package, recreate your virtual environment. Failing command: ['/opt/odoo/envname/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']**. – forvas Nov 29 '21 at 12:27
  • It might also help to check ` /opt/odoo/env/bin/python3.8 -m site` to see the available directories. – rocksteady Nov 29 '21 at 12:28
  • That returns this: ```sys.path = [ '/opt/odoo', '/usr/lib/python38.zip', '/usr/lib/python3.8', '/usr/lib/python3.8/lib-dynload', '/opt/odoo/env/lib/python3.8/site-packages', ] USER_BASE: '/home/anubia/.local' (exists) USER_SITE: '/home/anubia/.local/lib/python3.8/site-packages' (exists) ENABLE_USER_SITE: False``` – forvas Nov 29 '21 at 12:29
  • As @tripleee mentioned, I would also try and install `python3.8-venv`, then create the virtualenv using `python3 -m venv envname`. – rocksteady Nov 29 '21 at 12:29
  • I've installed `python3.8-venv` and afterwards I've created a new virtualenv with `python3 -m venv envname`. This new virtualenv has two command interpreters (python and python3) instead of three (python, python3 and python3.8). Besides, they're all symbolic links to the local Python command interpreters, so if that was the problem, it is not fixed. Odoo cannot work without `werkzeug` package, so I can't explain why, with my other virtualenv, Odoo runs OK, and you can work with it, but after a while, it falls down complaining about the `werkzeug` library. – forvas Nov 29 '21 at 13:06
  • @tripleee rocksteady After two days, the Odoo service has stopped again with exactly the same error, in spite of installing the virtualenv as you said. May be as ChesuCR answer says, it is not a problem of the virtualenv but the packages versions installed inside. I'll try that. Thank you! – forvas Dec 02 '21 at 11:21

3 Answers3

0

Odoo 13 is not compatible with the Python 3.8 version. You can try with Python 3.7 or 3.6 versions. Anyway, many issues have been fixed, so I am not sure about the Python 3.8 incompatibility. But, in principle, the branch Odoo v13 was created for Python 3.6, as you can check in the setup.py file

If the problem still bothers you, try to install the exact versions that Odoo developers posted in the requirements.txt file, where dependencies should have been tested. So you can install the right Werzeug version:

pip install Werkzeug==0.14.1

Ah, and if you want to show all the installed packages you can do:

pip list
ChesuCR
  • 9,352
  • 5
  • 51
  • 114
  • I'll try this and give you feedback after some days. Thank you ;) – forvas Dec 02 '21 at 11:22
  • Unfortunately, this solution seems not to work neither. I've edited my question. – forvas Dec 13 '21 at 12:43
  • I see, you have tried with Python3.7 as I can read in your edited question, try with python 3.6 and check if you get the same error or any other different problem – ChesuCR Dec 14 '21 at 12:07
  • God leads us in most unexpected ways. – ChesuCR Dec 14 '21 at 12:08
  • In fact, if you check the [`setup.py`](https://github.com/odoo/odoo/blob/13.0/setup.py#L59) you can chech that says v3.6. So, in principle, that branch was created for that version. – ChesuCR Dec 14 '21 at 12:29
  • I have edited my answer with this data :clap: :clap: – ChesuCR Dec 14 '21 at 12:31
  • Ok, I'll give a try to 3.6 version meanwhile. I'll give you the results in two weeks aprox I guess. – forvas Dec 14 '21 at 12:56
  • After trying it all this time, I got the same errors with 3.6. All of a sudden the service falls down complaining about a missing Python library which actually is already installed in the virtualenv. Same packages installed on the host do not give these errors. – forvas Feb 03 '22 at 10:35
0

I don't know what Odoo is, but in order to find the packages, you usually need to "activate" the virtualenv. Just like you did manually, but you also need that when Odoo is running the program. It is not enough to just call the correct python binary. So you can either try to have Odoo activate the venv before it starts the program, or if not possible, emulate doing that (it's probably mostly setting PATH and PYTHONPATH environment variables)

Gnurfos
  • 980
  • 2
  • 10
  • 29
  • But the venv docs (https://docs.python.org/3/library/venv.html) say the following: _You don’t specifically need to activate an environment; activation just prepends the virtual environment’s binary directory to your path, so that “python” invokes the virtual environment’s Python interpreter and you can run installed scripts without having to use their full path. However, all scripts installed in a virtual environment should be runnable without activating it, and run with the virtual environment’s Python automatically._ So, what should I do then? – forvas Dec 13 '21 at 16:18
  • It's the first time I see that doc, and I would intuitively interpret it as you are. But if read more litterally, it speaks about *running* a *script* *installed* in the venv, while you are trying to import modules installed there – Gnurfos Dec 13 '21 at 17:00
  • Do you mean that I should put my scripts inside the `virtualenv` folder? Because I think I should never do that. Pasting a whole ERP like Odoo inside a `virtualenv` is not a good idea. The `virtualenv` is not for holding customized projects inside, isn't it? – forvas Dec 13 '21 at 18:32
  • No, you should have odoo, before it runs the python stuff, set variables PYTHONPATH etc (by activating the venv) – Gnurfos Dec 13 '21 at 20:50
  • I have to find out some info about that to see how it works and try what you say. Thank you. – forvas Dec 14 '21 at 12:57
  • After trying a bit it seems that the venv works as advertised (as you thought). But I suspect that's only valid for the initial python process. If the odoo script then spawns additional processes (without doing some particular things to preserve the venv) the paths will get lost. Env variables might help – Gnurfos Dec 15 '21 at 20:27
0

For me, I had to fix it like this:

  1. First of all and a key point is that I had to use root user, not ubuntu not odoo nothing, only root
  2. I activated my venv in root user
  3. I installed required packages: pip3 install paramiko (or any other packages: boto3, or pretty-bad-protocol)

and then I could install the module from the odoo admin backend, and it's working just fine

dstream
  • 1
  • 1