pip --version returns SyntaxError (invalid syntax) after installation [closed]
OS: Debian 8
Python: v3.4.2 (uninstalled v2.7 and v3.5 in order to reduce possible root causes)
I installed pip for Python 3 as per documentation:
sudo apt install python3-venv python3-pip
Afterwards, checking the version results in a SyntaxError:
$ python3 -m pip --version
Traceback (most recent call last):
File "/usr/lib/python3.4/runpy.py", line 170, in _run_module_as_main
"__main__", mod_spec)
File "/usr/lib/python3.4/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/usr/local/lib/python3.4/dist-packages/pip/__main__.py", line 21, in <module>
from pip._internal.cli.main import main as _main
File "/usr/local/lib/python3.4/dist-packages/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
Please note the path to Python version 3.4, while the following check points to version 3.5:
$ pip --version
Traceback (most recent call last):
File "/usr/local/bin/pip", line 7, in <module>
from pip._internal.cli.main import main
File "/usr/local/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 60
sys.stderr.write(f"ERROR: {exc}")
^
SyntaxError: invalid syntax
However, which pip points to another path:
$ which pip
/usr/local/bin/pip
What is going wrong? How can I fix PIP?
I need it to install the tool elastalert.
What I have tried so far:
- Uninstalled PIP for Python 3.5 and checked version again.
- Uninstalled PIP for Python 2.7 and checked version again.
- Uninstalled Python v3.5 (
apt remove --purge
) and checked version again. - Uninstalled Pyton v2.7 and checked version again.
- Uninstalled Python v3.4 and checked version again.
- Re-installed Pyhton v3.4 and checked version again.
Solution 1:
According to phd's post, the root cause is the PIP version installed by default being not compatible with the old Python version 3.4. The crucial clue was the f""
being only supported by Python v3.6+.
So I've uninstalled PIP and re-installed PIP 19.1.1 as follows:
-
Uninstall incompatible PIP provided by distro repository:
sudo apt remove --purge python3-pip
-
Download a
get-pip.py
version which is compatible with my Python version 3.4:curl -O https://bootstrap.pypa.io/pip/3.4/get-pip.py
-
Install PIP (in my case with
sudo
as global installation and with-E
to apply proxy settings tosudo
session):sudo -E python3 get-pip.py
-
Optional: Upgrade PIP to ensure it's the latest available version:
sudo -E python3 -m pip install --upgrade "pip < 19.2"
-
Check version:
python3 -m pip --version
Ouput:
pip 19.1.1 from /usr/local/lib/python3.4/dist-packages/pip (python 3.4)