Search code examples
pythonmacospycharmvirtualenv

Pycharm on Mac: why Python SDK cache get corrupted regularly?


This is the massage that Pycharm gives me almost once a day, and I have to restart it. As I have multiple projects open, it gives this error for each virtualenv repeatedly until I force quit it.

Is there a way to prevent Pycharm from constantly invalidating cache?

#ps: I never had such issues with Pycharm on Windows

Invalid Python SDK

Cannot set up a python SDK at Python 3.9 (demographics-g5XoraTQ) (/Users/mamad/Library/Caches/pypoetry/virtualenvs/up-demographics-g5XoraTQ-py3.9/bin/python). The SDK seems invalid.

pycharm error


Solution

  • It turns out the issue was the number of git repositories each with a separate project SDK (i.e. venv interpreter) that I had simultaneously opened in my PyCharm instance (over 10).

    The re-indexing of git caches and Python libraries created memory issues and eventually resulted in corruption of index files; this couldn't be solved unless I restarted my PyCharm instance once a day.

    The solution was to:

    • Either re-use one virtual environment for all projects; which is not desirable at all.
    • Or, as soon as I am done with a project, remove the project along with its Python interpreter and git repository from PyCharm. To speed things up, I set my Poetry to create its .venv inside the project folder; so interpreter and project both can be removed in a single command.

    One undesirable outcome of the second solution is that my shell now displays one venv name for all my virtual environments in all project folders.