Mark Kortink
Mark Kortink

Reputation: 2120

python pip corrupted, can't repair or uninstall

Due to a series of events described later I am getting the following error in the command prompt whenever I try to do anything with pip.

(venv) (base) C:\Users\Mark Kortink\Dropbox\Python\projects\metapplica>pip install flask
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "C:\ProgramData\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\MARKKO~1\Dropbox\Python\projects\METAPP~1\venv\Scripts\pip.exe\__main__.py", line 9, in <module>
  File "c:\users\markko~1\dropbox\python\projects\metapp~1\venv\lib\site-packages\pip\_internal\main.py", line 45, in main
    command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
  File "c:\users\markko~1\dropbox\python\projects\metapp~1\venv\lib\site-packages\pip\_internal\commands\__init__.py", line 96, in create_command
    module = importlib.import_module(module_path)
  File "C:\ProgramData\Anaconda3\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "c:\users\markko~1\dropbox\python\projects\metapp~1\venv\lib\site-packages\pip\_internal\commands\install.py", line 23, in <module>
    from pip._internal.cli.req_command import RequirementCommand
  File "c:\users\markko~1\dropbox\python\projects\metapp~1\venv\lib\site-packages\pip\_internal\cli\req_command.py", line 17, in <module>
    from pip._internal.index import PackageFinder
ImportError: cannot import name 'PackageFinder' from 'pip._internal.index' (c:\users\markko~1\dropbox\python\projects\metapp~1\venv\lib\site-packages\pip\_internal\index\__init__.py)

All the circumstances are described in my other question which didn't get a useful answer. It is here ModuleNotFoundError.

I a nutshell: -

  1. I had an working app in Flask running in an Anaconda environment with everything installed using pip (not conda), it used flask-bootstrap.
  2. I uninstalled flask-bootstrap and installed a different package Bootsrap-flask to get bootstrap-4, it worked.
  3. I uninstalled Bootstrap-flask because I decided to go pure CSS.
  4. I can still run my app from the Anaconda environment in its venv using "flask run".
  5. But as soon as I try and run anything flask related in a debugger (Spyder or VSCode) i get that core Flask libraries cannot be found.
  6. And as soon as I do anything with pip I get the above error.

I believe all the detail above is a distraction, I have incuded it for completeness and it is covered in my other linked question. I believe the root cause should be apparent from the above read-out from the command prompt. In particular ImportError: cannot import name 'PackageFinder' from 'pip._internal.index'

Can anyone suggest anything. If not how do I safely and cleanly remove my Flask venv environment and reinstall it without losing anything.

Thanks

Upvotes: 2

Views: 9963

Answers (2)

Mark Kortink
Mark Kortink

Reputation: 2120

For the record this is what I ended up doing.

  1. Uninstalled Anaconda.
  2. Switched to Flask app directory and deactivated then deleted venv.
  3. Installed latest version of Python.
  4. Installed virtualenv.
  5. Switched to Flask app directory and created and activated a new venv.
  6. Installed Flask and all the other bits and pieces one by one with pip.
  7. Installed the WinPython app (no more Anaconda).
  8. Very pleased with WinPython, it is natural and simple rather than a opinionated and complex like Anaconda (think Flask vs Django).
  9. Spyder worked, but old version.
  10. Upgraded Spyder the WinPython way using pip, it worked.

The Flask app works and I don't get errors when I run pip. I am back to my original "missing module" problem but now I understand how the environment is set up I think I can sort that out. Will post an answer to that question when I do.

Upvotes: 1

Anthony Kimanzi
Anthony Kimanzi

Reputation: 856

This should serve as a tip and answer to your problem. I have worked with flask extensively and simple mess in your environment can mess your entire project and end up eating all your time while you are trying to get a fix online.If you encounter environment problem always recreate your project again. I would recommend you to work with Pycharm IDE because Anaconda environment can be mess up sometimes. follow the steps below to fix your issue

Fix 1

  1. Open your anaconda prompt and Run this command on

python -m pip --version

  1. After confirming the Pip Version and its location run an upgrade

python -m pip install --upgrade pip

  1. To make sure everything is working fine run

conda update pip

Fix 2 Seems there is a problem with your packages

  1. create a requirements.txt file in working directory
  2. Automatically pass all your required packages to your requirements.txt by using the code below on python terminal

pip freeze > requirements.txt

  1. Run the command below to install packages you might be missing or corrupted

pip install -r requirements.txt

All the best

Upvotes: 1

Related Questions