1

How do I fix this? I was trying to upgrade my Python to 3.10.2 which works fine. However, pip is throwing errors.

These are the steps I followed to upgrade:

#Downloaded new version and Extracted source files
wget https://www.python.org/ftp/python/3.10.2/

#Ran the configuration command
./configure --enable-optimizations  

#Installed any missing dependencies 
sudo apt update
sudo apt install -y build-essential tk-dev libncurses5-dev     libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev

#Compiled Python
sudo make altinstall

#Link the python to new version
cd /usr/bin
sudo rm python3
sudo ln -s /usr/local/bin/python3.10 python3

Upgrade to Python 3.10.2 worked:

pi@raspberrypi:~ $ python3 --version 
Python 3.10.2
pi@raspberrypi:~ $ which python3
/usr/bin/python3

pi@raspberrypi:~ $ which python
/usr/bin/python
pi@raspberrypi:~ $ python --version
Python 2.7.13

Now when trying to run pip I get errors:

pi@raspberrypi:~ $ pip3 list
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
Package    Version
---------- -------
pip        21.2.4
setuptools 58.1.0

pi@raspberrypi:~ $ python3 -m pip --version
    pip 21.2.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)

pi@raspberrypi:~ $ python3 -m pip list
Package    Version
---------- -------
pip        21.2.4
setuptools 58.1.0
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/runpy.py", line 196

Error when trying to upgrade pip:

pi@raspberrypi:/usr/local/lib/python3.10/site-packages $ python -m pip install --upgrade pip
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 163, in _run_module_as_main
mod_name, _Error)
File "/usr/lib/python2.7/runpy.py", line 111, in _get_module_details
__import__(mod_name)  # Do not catch exceptions initializing     package
File "pip/__init__.py", line 6
def main(args: Optional[List[str]] = None) -> int:
             ^
SyntaxError: invalid syntax

NB: I've had this Raspberry Pi for a while and never updated the OS. It has Raspbian GNU/Linux 9 (stretch).

3
  • 3
    have you visited github.com/pypa/pip/issues/5599 ? have you tried python -m pip ... as suggested? Commented Sep 16, 2023 at 6:53
  • What OS are you running? You have Python 2.7.13 so this must be old! You would be better installing an up-to-date OS. Raspberry Pi OS Bulseye has Python 3.9.2 Raspberry Pi OS Bookworm (which is due shortly) will have Python 3.11.2
    – Milliways
    Commented Sep 17, 2023 at 6:31
  • Yes - I've had this for awhile and never updated the OS. It has Raspbian GNU/Linux 9 (stretch). Would an upgrade fix this issue? Or are you suggesting a clean install? Thanks!
    – CornDevil
    Commented Sep 17, 2023 at 14:18

1 Answer 1

0

The warning message gives you some hints about the problem:

WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.

If you visit the link provided in the message, you'll find a lot of useful information. It starts with (emphasis mine):

After upgrading to pip 10 or higher, many users are encountering [certain] errors.

These are caused by an incorrect attempt to upgrade pip, which has typically resulted in (parts of) multiple versions of pip being installed in the same Python installation, and those parts being incompatible.

It should be noted that these issues are invariably not problems with pip itself, but are due to incorrect use of pip, or unexpected interactions with system scripts that are not controlled by pip.

In your latest error message, you see a SyntaxError in a file pip/__init__.py that is called from /usr/lib/python2.7/runpy.py. The pip file contains type hints, which were introduced in Python 3.5. So this pip installation is definately from Python version 3.5 or later. This confirms that Python versions are somehow mixed in your installation. Since Python 2.x and 3.x are not compatible, it is not surprising that this will result in errors.

The link mentioned above also explains how to fix the issue

Fixing the Issue

The problem, of course, is fixing the issue. And that's where you really are on your own. If you have changed your system installation, you really need to put it back the way that the distribution installed it. That may well require an uninstall and reinstall of your distribution's pip package. Reinstalling is easy, of course - but uninstalling may require manually removing incorrect files. Or you may be able to force-reinstall with your distribution package manager, simply overwriting the incorrect files. Of course, this reverts you to the system-supplied version of pip.

So my advice would be to completely uninstall and reinstall your Python installations. Make sure to follow the right procedure when updating Python packages (such as pip) — do not call pip but python -m pip instead.

Depending on your use case, it might even be easier to completely reinstall your Raspberry Pi. If you did not install a lot of software on it already, this will likely be the easiest and fastest solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.