'How to fix a bad upgrade of pip on python3

Before someone slaps my hand, I had installed venv/virtualenv to setup virtual environments but I had other issues so someone suggested I upgrade my pip3 version to the current version (22.0.4 right now)

I somehow messed up both pip and pip3 so hopefully someone can help me.

I tried to upgrade my version of pip on my Ubuntu 16.04 box by running

pip3 install --upgrade pip

I also tried running

pip3 install --upgrade pip3

After upgrading, I kept getting errors when trying to run pip3 so I removed it, planning to install with the get-pip.py script.

However, now when I try to install pip3 using

sudo python3 /tmp/get-pip.py

I keep getting an AttributeError:

$ sudo python3 /tmp/get-pip.py 
Traceback (most recent call last):
  File "/tmp/get-pip.py", line 27077, in <module>
    main()
  File "/tmp/get-pip.py", line 135, in main
    bootstrap(tmpdir=tmpdir)
  File "/tmp/get-pip.py", line 111, in bootstrap
    monkeypatch_for_cert(tmpdir)
  File "/tmp/get-pip.py", line 92, in monkeypatch_for_cert
    from pip._internal.commands.install import InstallCommand
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "/tmp/tmpu1lmh2fr/pip.zip/pip/_internal/commands/__init__.py", line 9, in <module>
...
...
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 656, in _load_unlocked
  File "<frozen importlib._bootstrap>", line 626, in _load_backward_compatible
  File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 5677, in <module>
  File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1488, in setResultsName
  File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1491, in _setResultsName
  File "/tmp/tmpu1lmh2fr/pip.zip/pip/_vendor/pyparsing.py", line 1445, in copy
AttributeError: module 'copy' has no attribute 'copy'

Can someone point me to instructions on how to re-install pip/pip3 or how to get around this AttributeError? Hopefully the solution will also keep my old modules that I had downloaded from before too. Thanks



Solution 1:[1]

Check if there is a file /tmp/copy.py or directory+file /tmp/copy/__init__.py. Try these commands: sudo python3 -c "import copy; print(copy.copy)" and sudo python3 -c "import copy; print(copy.__file__)"

If any of those exists it will be imported instead of standard module copy because Python prepends the directory of a script (the script is /tmp/get-pip.py, the directory /tmp/) at the beginning of sys.path so it's where Python starts to find modules/packages.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 phd