Cannot setup a Python SDK in PyCharm project using virtualenv after OS reinstallation

Cannot setup a Python SDK in PyCharm project using virtualenv after OS reinstallation

What solution is: Check out venvpyvenv.cfg and provide a valid path to the basic python installation.

What has most probably happend:
After reinstalling your OS, you have no base python interpreter reinstalled or you have installed it at different location than before. Thus your virtual environment fails to locate the python installation. Virtual environment implies that all libraries and settings are isolated from other projects. It does not provide an isolated python installation. You still need your base python that had been used for venv creation.

My case: I have a project in a network share and try accessing it from different computers. The base python paths depend on the very PC. The solution above works fine for me. Unfortunatelly, I need to update pyvenv.cfg depending on the PC in use.

P.S. I believe that there is an environment variable to overide the venv config value. I only tried to set PYTHONPATH=C:Anaconda3envspython37 on Windows and then to activate venv. It did no effect and I gave it up.

Heres what solved my problem when I faced the exact same issue

Navigate to Project Interpreter, right side of the selection box, click the gear icon, it will show two options add & show all.

Click show all, if you see the previous existence from the same directory delete that. Click on add or + sign to add a new interpreter and navigate to your project path, navigate down to the virtual environment directory.

venv/bin/{select the python executable with the version code i.e if you are using python3.6 select python3.6}

Once selected, you can now click the notice that says install packaging tools, hit apply and done.

enter

Cannot setup a Python SDK in PyCharm project using virtualenv after OS reinstallation

Most probably, some path to Python environment that PyCharm tries to use has become invalid somewhere. There are (at least) two primary suspects:

Path to your virtualenv in PyCharm settings

PyCharm needs to know the path to your environment to run things in it. So, if that path changed, PyCharms saved path has become invalid.

Go to the interpreter settings for your project in File->Settings...->Project interpreter->(Gear icon)->Show all...:

settings

Then delete and recreate the necessary entries. Or edit them and specify correct paths.
E.g. this is what my list looks like after I deleted an Anaconda installation:

interpreter

Path in the virtualenv to its base installation

Since virtualenv is not a full installation, it must have a path to its base installation stored somewhere to be able to use files from there.

As of this writing, virtualenv (v16.0.0) in Windows is implemented like this:

  • The real python.exe and several other files are copied into the virtualenvs subtree
  • In Liborig-prefix.txt, the path to the base installation is stored. It is used to add the base installations Lib to sys.path via a custom site.py.

So, if the path in that file becomes invalid, the virtualenvs Python interpreter will be unable to find any standard modules except those few that were copied. Which perfectly fits your symptoms.

Leave a Reply

Your email address will not be published.