Pycharm error Non-zero exit code (2) - solution

foreword

Using PyCharm inPython InterpreterWhen installing a third-party package in the Python virtual environment in the settings , it is likely to report an error:Non-zero exit code (2), as shown in the figure:
insert image description here
Even upgrading pip will report an error, as shown in the figure:
insert image description here

The tips are as follows:

Try to run this command from the system terminal. 
Make sure that you use the correct version of 'pip' installed for your Python interpreter located at 
'D:\Program Files\Python\py36-basic-v\Scripts\python.exe'.
  • 1
  • 2
  • 3

As mentioned above, the official said that the possible reason ispipis not installed in the specified directory of the virtual environment, and it is recommended to run the pip command in the terminal to install third-party packages.

The official prompt is reasonable to a certain extent, but not all cases are tried. You can check whether the pip module exists in the directory of the virtual environment.pipThe module can be installed in the specified directory; if it exists, it is notpipThe problem with the module path, so what is the reason?

After investigation, yespipThe problem of version, my Python version is 3.6, pip version is 21.3.1, this version ofpipThis problem must occur, and the solution is also very simple, thepipThe version can be downgraded to 20.2.4. For the operation method, see Solution 2 below: Downgrade the pip version . (It is not recommended to upgrade pip, because new versions of pip may have the same problem)

Option 1 (recommended): Use Terminal to install third-party packages

Click on the toolbar at the bottom of PyCharmTerminal, as shown below:
insert image description here
Inputpip installCommand to install the third-party package, as shown in the figure:
insert image description here
But this solution does not solve the problem, the problem has not been solved, but the installation of the third-party package is carried out through an alternative method. If you want to solve the problem fundamentally, please refer to the solution 2 below .

Option 2 (fundamental solution): downgrade pip version

First open a project with PyCharm. If there is no Python environment, you need to create a virtual environment first, as shown below:
insert image description here

existFile - Settings - Python InterpreterYou can see that the version of pip in the Python virtual environment is 21.3.1, as shown in the figure below, we need to downgrade the version of pip to 20.2.4.
insert image description here
Click on the toolbar at the bottom of PyCharmTerminal, as shown below:
insert image description here
Enter the following command, note that there must bepython -m, otherwise no permissions:

python -m pip install pip==20.2.4
  • 1

After entering the command, the following prompt appears to prove that the downgrade was successful:
insert image description here
Enter the commandpip -VYou can view the current pip version, as shown below:
insert image description here
So far the problem has been successfully solved, inSettingsThe third-party package can be successfully installed in the installation, as shown below:
insert image description here

Related: Pycharm error Non-zero exit code (2) - solution