> For the complete documentation index, see [llms.txt](https://hanxu.gitbook.io/blogs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hanxu.gitbook.io/blogs/uncategorized/pycharm-jie-jue-xu-ni-huan-jing-pip-wu-fa-shi-yong-wen-ti.md).

# 【PyCharm】解决虚拟环境pip无法使用问题

@[TOC](https://github.com/UnderTurrets/notes/blob/master/uncategorized/文章目录/README.md)

***

## 1.新建虚拟环境的bug

### 描述

* 在PyCharm内新建或者打开一个项目时，我们需要选择一个python解释器，如果我们选择新建一个虚拟环境，如下图所示：

![](https://cdn.jsdelivr.net/gh/UnderTurrets/ImageHosting@master/pycharm_newPythonEnv.png)

* 进入项目后，我们打开终端，可以看到自动进入了虚拟环境：

```shell
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

安装最新的 PowerShell，了解新功能和改进！ https://aka.ms/PSWindows

(venv) PS D:\Desktop\pycharm-projects\ExForTeach>
```

* 但是，**如果我们尝试使用pip命令，会报错：**

```shell
(venv) PS D:\Desktop\pycharm-projects\ExForTeach> pip list
Traceback (most recent call last):
  File "C:\Users\Xu Han\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "C:\Users\Xu Han\AppData\Local\Programs\Python\Python39\lib\runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "D:\Desktop\pycharm-projects\ExForTeach\venv\Scripts\pip.exe\__main__.py", line 4, in <module>
  File "D:\Desktop\pycharm-projects\ExForTeach\venv\lib\site-packages\pip\_internal\cli\main.py", line 9, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "D:\Desktop\pycharm-projects\ExForTeach\venv\lib\site-packages\pip\_internal\cli\autocompletion.py", line 10, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "D:\Desktop\pycharm-projects\ExForTeach\venv\lib\site-packages\pip\_internal\cli\main_parser.py", line 8, in <module>
    from pip._internal.cli import cmdoptions
  File "D:\Desktop\pycharm-projects\ExForTeach\venv\lib\site-packages\pip\_internal\cli\cmdoptions.py", line 29, in <module>
    from pip._internal.models.target_python import TargetPython
ModuleNotFoundError: No module named 'pip._internal.models.target_python'
```

### 解决方法

* 这时，我们只需要**删除图中的文件夹**：

![](https://cdn.jsdelivr.net/gh/UnderTurrets/ImageHosting@master/pycharm_solve_venv.png)

* 然后再尝试使用pip命令：

```shell
(venv) PS D:\Desktop\pycharm-projects\ExForTeach> pip list
Package            Version
------------------ ---------
bleach             6.0.0
certifi            2022.12.7
chardet            5.1.0
charset-normalizer 2.1.1
contourpy          1.0.7
cycler             0.11.0
docutils           0.19
fonttools          4.38.0
idna               3.4
importlib-metadata 6.5.0
importlib-resources 5.12.0
jaraco.classes     3.2.3
keyring            23.13.1
kiwisolver         1.4.4
markdown-it-py     2.2.0
matplotlib         3.7.0
mdurl              0.1.2
more-itertools     9.1.0
numpy              1.24.1
```

* 可以看到pip已经可以正常使用了！
* 由于我继承了全局站点，因此这里我的软件包很多

## 2.更换路径的bug

* 前几天，我将项目的路径从`D:\Desktop\pycharm-projects\venv1`改成`C:\Users\Xu Han\Desktop\pycharm-projects\venv1`，然后尝试在虚拟环境中`pip list`，报错如下：

```shell
Fatal error in launcher: Unable to create process using '"D:\Desktop\pycharm-projects\venv1\venv\Scripts\python.exe"  "C:\Users\Xu Han\Desktop\pycharm-projects\venv1\venv\Scripts\pip.exe" list': ???????????
```

### 解决方法

![](https://cdn.jsdelivr.net/gh/UnderTurrets/ImageHosting@master/pycharm_solve_pathError1.png)

* 在pycharm中，**右击`venv\Scripts`这个文件夹**，查找替换成现在的路径即可，如下：

![](https://cdn.jsdelivr.net/gh/UnderTurrets/ImageHosting@master/pycharm_solve_pathError2.png)

* 然后重新安装pip，由于这里是虚拟环境，所以`python -m ensurepip`是不管用的

```shell
(venv) PS C:\Users\Xu Han\Desktop\pycharm-projects\venv1> python -m ensurepip
Looking in links: c:\Users\XUHAN~1\AppData\Local\Temp\tmpr6ouww8y
Requirement already satisfied: setuptools in c:\users\xu han\desktop\pycharm-projects\venv1\venv\lib\site-packages (60.2.0)
Requirement already satisfied: pip in c:\users\xu han\appdata\local\programs\python\python39\lib\site-packages (23.1)
```

* 需要先尝试卸载：

```shell
(venv) PS C:\Users\Xu Han\Desktop\pycharm-projects\venv1> python -m pip uninstall pip
```

如果你遇到了第一个bug，已经删除了`venv\Lib\site-packages\pip`和`venv\Lib\site-packages\pip-23.3.1.dist-info`，就不用卸载了

* 然后尝试升级`python.exe -m pip install --upgrade`，升级的途中会自动安装：

```shell
(venv) PS C:\Users\Xu Han\Desktop\pycharm-projects\venv1>  python.exe -m pip install --upgrade pip
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Requirement already satisfied: pip in c:\users\xu han\appdata\local\programs\python\python39\lib\site-packages (23.1)
Collecting pip
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/47/6a/453160888fab7c6a432a6e25f8afe6256d0d9f2cbd25971021da6491d899/pip-23.3.1-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
  Attempting uninstall: pip
    Found existing installation: pip 23.1
    Not uninstalling pip at c:\users\xu han\appdata\local\programs\python\python39\lib\site-packages, outside environment C:\Users\Xu Han\Desktop\pycharm-projects\venv1\venv
    Can't uninstall 'pip'. No files were found to uninstall.
Successfully installed pip-23.3.1

[notice] A new release of pip is available: 23.1 -> 23.3.1
[notice] To update, run: python.exe -m pip install --upgrade pip
```

## 2.虚拟环境使用

### 2.1 特性

* **在虚拟环境中安装软件包完全不影响全局软件包**
* **只要选择继承全局站点，虚拟环境就可以自由地使用全局软件包**
* 如图，我在虚拟环境中安装了scipy，但是在全局中并没有这个软件包：

```shell
scipy      1.10.1
setuptools 60.2.0
six        1.16.0
```

```shell
setuptools 67.7.1
six        1.16.0
torch      1.13.1+cu116
```

### 2.2 进入与退出

#### 进入

```shell
PS C:\Users\Xu Han\Desktop\pycharm-projects\venv1> ./venv/Scripts/activate
```

#### 退出

```shell
(venv) PS C:\Users\Xu Han\Desktop\pycharm-projects\venv1> ./venv/Scripts/deactivate
```
