I do run pylint
in unittests using subprocess.run()
.
I need to use --init-hook=
feature of pylint
to encourage it to find my modules.
I do run this on shell (bash) and it works as expected:
pylint qttools.py --disable=all --enable=E0401 --init-hook="from pathlib import Path; import sys;sys.path.append('./../common');"
No error E0401 detected.
Now I try to replicate that but using subprocess.run()
.
Python 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from subprocess import run
>>> cmd = ['pylint', 'qttools.py', '--disable=all', '--enable=E0401']
>>> cmd.append('--init-hook="from pathlib import Path; import sys;sys.path.append(\'./../common\');"')
>>> run(cmd)
************* Module qttools
qttools.py:48:0: E0401: Unable to import 'snapshots' (import-error)
qttools.py:49:0: E0401: Unable to import 'tools' (import-error)
qttools.py:50:0: E0401: Unable to import 'logger' (import-error)
-------------------------------------------------------------------
Your code has been rated at 9.55/10 (previous run: 10.00/10, -0.45)
CompletedProcess(args=['pylint', 'qttools.py', '--disable=all', '--enable=E0401', '--init-hook="from pathlib import Path; import sys;sys.path.append(\'./../common\');"'], returncode=2)
My assumption is that escaping the '
around the common
path is a problem. I tried some alternatives (switching '
and "
; building the string via +
operator). The result keeps the same.
Any ideas?
I am aware that I wouldn't need to use --init-hook
or sys.path
hacks like this if I created a correct and valid Python package out of this repo. I am working on this, but it is 15 year old Python code.
I got it. The problem was that I surrounded the value of --init-hook
by "
.
>>> cmd.append('--init-hook="from pathlib import Path; import ..."')
^ ^
Removing it works fine.
>>> cmd.append('--init-hook=from pathlib import Path; import sys;sys.path.append(\'./../common\');')