2

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

davidism
  • 121,510
  • 29
  • 395
  • 339
Mark Kortink
  • 1,770
  • 4
  • 21
  • 36

2 Answers2

1

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

Kali Kimanzi
  • 856
  • 7
  • 16
  • Why mix pip and Conda like this? – AMC Mar 18 '20 at 23:59
  • it doesn't make a difference is only that you cant use conda command on the normal cmd or powershell – Kali Kimanzi Mar 19 '20 at 00:59
  • OK, thanks @KaliKimanzi here are the results. Fix 1: (1) version command works 19.3.1; (2) fails with above error; (3) conda update works. Fix 2: (2) works, requirements file creates; (3) fails with error above. – Mark Kortink Mar 19 '20 at 23:53
  • Thanks @AMC, short answer is I had no idea what I was doing when I got started about 12 months ago. Started messing with Anaconda for data science reasons so just installed it lock, stock and barrel. Several months later I did Miguel Grinbergs Flask Megatutorial and just used the conda command prompt not knowing any better. I did start to realise things were screwy but everything worked and I had better things to do than sort my environment. Swapping bootstrap modules brought it all crashing down :( – Mark Kortink Mar 19 '20 at 23:57
  • I would like to get to a scenario where I have completely separate environments for Anaconda and my Flask development. I am pretty confident I have set up a "normal" python environment on my Win10 laptop. The question then is how do I move my Flask app to it and optionally clean up the Anaconda environment by removing Flask etc? – Mark Kortink Mar 20 '20 at 00:00
  • @KaliKimanzi _it doesn't make a difference is only that you cant use conda command on the normal cmd or powershell_ I'm pretty sure neither of those things are true. The first part is completely wrong, see, for example: https://www.anaconda.com/using-pip-in-a-conda-environment/. As for the second, [this answer](https://stackoverflow.com/a/58211115/11301900) shows how to do just that. – AMC Mar 20 '20 at 00:14
  • @MarkKortinkc _I would like to get to a scenario where I have completely separate environments for Anaconda and my Flask development._ Why is that? Is it particularly difficult to use the two together? _The question then is how do I move my Flask app to it and optionally clean up the Anaconda environment by removing Flask etc?_ Unfortunately I don't think I know enough about Flask to be able to help with that. Do you think it would be realistic to keep the Flask project in place while you clean up and wipe all traces of Anaconda, and then reinstall it again? – AMC Mar 20 '20 at 00:20
1

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.

Mark Kortink
  • 1,770
  • 4
  • 21
  • 36