文章目录
  • 一、报错信息
  • 二、问题分析
  • 三、解决方案
  • 1、解决方案一 修改环境变量 ( 不推荐 | 没有解决问题 )
  • 2、解决方案二 修改 PyCharm 中使用的 编译器版本 ( 推荐 | 成功解决问题 )







一、报错信息



在 PyCharm 中的 Python 项目中 , 使用了 PyTorch 库 , 提示

No module named 'torch'
  • 1.

这里直接点击错误提示下的 " Install package torch " 选项 ,

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_python

执行后 , 弹出如下报错信息 :

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_ide_02



报错信息 :

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
  • 1.
  • 2.
  • 3.

详细报错信息如下 :

stream
data = self.read(amt=amt, decode_content=decode_content)
File “D:\001_Develop\022_Python\Python39\lib\site-packages\pip_vendor\urllib3\response.py”, line 541, in read
raise IncompleteRead(self._fp_bytes_read, self.length_remaining)
File “D:\001_Develop\022_Python\Python39\lib\contextlib.py”, line 137, in exit
self.gen.throw(typ, value, traceback)
File “D:\001_Develop\022_Python\Python39\lib\site-packages\pip_vendor\urllib3\response.py”, line 443, in _error_catcher
raise ReadTimeoutError(self._pool, None, “Read timed out.”)
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host=‘files.pythonhosted.org’, port=443): Read timed out.
WARNING: You are using pip version 21.2.3; however, version 24.2 is available.
You should consider upgrading via the ‘D:\001_Develop\022_Python\Python39\python.exe -m pip install --upgrade pip’ command.



上述错误提示中 , 提示执行

D:\001_Develop\022_Python\Python39\python.exe -m pip install --upgrade pip
  • 1.

命令 , 可以解决该问题 , 但是执行 上述命令 , 也是报相同的错误 , pip 命令就是不能执行 ;






二、问题分析



之前在 命令行 中使用了 pip install torch 命令 , 安装了 PyTorch 库 , 安装成功 , 输出内容如下 :

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_pip_03

在 PyCharm 中 , 选择

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_PyTorch_04

在 Settings 窗口中的 Python Interpreter 选项卡中 , 查看当前使用的 编译器版本是 3.9 ;

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_ide_05

在 cmd 中 , 执行

pip --version
  • 1.

命令 , 查看当前的 pip 版本号 , 输出内容如下 :

C:\Users\octop>pip --version
pip 20.1.1 from d:\001_develop\022_python\python37_64\lib\site-packages\pip (python 3.7)
  • 1.
  • 2.

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_pip_06

在 Windows 环境变量中配置的 Python 版本是 3.7 版本的 , 使用的 pip 工具也是 3.7 版本的 , 出现了冲突 ;

PyCharm 中使用的 Python 版本是 3.9 版本的 , 这里出现了冲突 , 导致软件包安装错误 ;

不同的 Python 版本 对应的 软件包 版本也是不同的 ;

把我电脑上的 Path 环境变量 拉出来 , 吓一跳 ,

%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;C:\Windows\System32;%SystemRoot%\system32;C:\Program Files\PlasticSCM5\server;C:\Program Files\PlasticSCM5\client;D:\001_Develop\022_Python\Python37_64\Scripts\;D:\001_Develop\022_Python\Python37_64\;D:\001_Develop\022_Python\Python27;D:\001_Develop\022_Python\Python310\Scripts\;D:\001_Develop\022_Python\Python310\;D:\001_Develop\022_Python\Python39\Scripts\;D:\001_Develop\022_Python\Python39\;D:\001_Develop\022_Python\Python38\Scripts\;D:\001_Develop\022_Python\Python38\;D:\001_Develop\022_Python\Python36\Scripts\;D:\001_Develop\022_Python\Python36\;C:\ProgramData\Oracle\Java\javapath;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;%SYSTEMROOT%\System32\OpenSSH\;C:\Program Files\Java\jdk1.8.0_91\bin;D:\001_Develop\001_SDK\Sdk\platform-tools;C:\Strawberry\c\bin;C:\Strawberry\perl\site\bin;C:\Strawberry\perl\bin;D:\001_Develop\010_Flutter\flutter_windows_2.2.3-stable\flutter\bin;%ANDROID_HOME%\emulator;%ANDROID_HOME%\platform-tools;%ANDROID_HOME%\tools;D:\001_Develop\001_SDK\Sdk\build-tools\30.0.3;D:\001_Develop\020_TDM-GCC-64\bin;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;D:\001_Develop\007_Git\Git\cmd;D:\001_Develop\035_Groovy\apache-groovy-sdk-3.0.9\groovy-3.0.9\bin;C:\Program Files (x86)\Common Files\Propellerhead Software\ReWire\;C:\Program Files\Common Files\Propellerhead Software\ReWire\;D:\001_Develop\037_Matlab\MATLAB\R2017a\runtime\win64;D:\001_Develop\037_Matlab\MATLAB\R2017a\bin;E:\Program Files\7-Zip;C:\Program Files\dotnet\;D:\Program Files\Redis\;C:\Program Files (x86)\Microsoft SQL Server\160\DTS\Binn\;C:\Program Files\Azure Data Studio\bin;%HADOOP_HOME%\bin;%HADOOP_HOME%\sbin;%USERPROFILE%\.dnx\bin;C:\Program Files\Microsoft DNX\Dnvm\;D:\001_Develop\053_Huawei\Sdk\hmscore\3.1.0\toolchains;D:\001_Develop\053_Huawei\nodejs\nodejs-16.20.1
  • 1.

配置的 Python 相关的 环境变量如下 :

D:\001_Develop\022_Python\Python37_64\Scripts\;D:\001_Develop\022_Python\Python37_64\;D:\001_Develop\022_Python\Python27;D:\001_Develop\022_Python\Python310\Scripts\;D:\001_Develop\022_Python\Python310\;D:\001_Develop\022_Python\Python39\Scripts\;D:\001_Develop\022_Python\Python39\;D:\001_Develop\022_Python\Python38\Scripts\;D:\001_Develop\022_Python\Python38\;D:\001_Develop\022_Python\Python36\Scripts\;D:\001_Develop\022_Python\Python36\;
  • 1.

按行分割 ;

D:\001_Develop\022_Python\Python37_64\Scripts\;
D:\001_Develop\022_Python\Python37_64\;
D:\001_Develop\022_Python\Python27;
D:\001_Develop\022_Python\Python310\Scripts\;
D:\001_Develop\022_Python\Python310\;
D:\001_Develop\022_Python\Python39\Scripts\;
D:\001_Develop\022_Python\Python39\;
D:\001_Develop\022_Python\Python38\Scripts\;
D:\001_Develop\022_Python\Python38\;
D:\001_Develop\022_Python\Python36\Scripts\;
D:\001_Develop\022_Python\Python36\;
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

环境变量装太多了 , 第一个 Python 环境变量是 3.7 的 , 默认先试用这个环境变量 ;

将 3.9 的环境变量提到最前面 , 或者将 PyCharm 中的 Python 编译器版本改为 3.7 , 都可以解决上述问题 ;






三、解决方案




1、解决方案一 修改环境变量 ( 不推荐 | 没有解决问题 )



PyCharm 中使用的是 3.9 的环境变量 ,

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_pycharm_07

D:\001_Develop\022_Python\Python39\Scripts\;D:\001_Develop\022_Python\Python39\;
  • 1.

环境变量 , 提升到 Path 环境变量的最前面 ;

可解决上述问题 ;

修改后 , 重启一个新的命令提示符 , 执行

pip --version
  • 1.

命令 , 可以看到 当前使用的是 Python 3.9 的 pip 工具 ;

C:\Users\octop>pip --version
pip 21.2.3 from D:\001_Develop\022_Python\Python39\lib\site-packages\pip (python 3.9)
  • 1.
  • 2.

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_pycharm_08

在命令行中 , 还是报相同的错误 , 没有解决问题 ;



2、解决方案二 修改 PyCharm 中使用的 编译器版本 ( 推荐 | 成功解决问题 )



在 Python 编译器 选项中的下拉菜单中没有看到 3.7 选项 , 这里需要先进行配置 ;

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_python_09

在 下面的对话框中 , 点击 + 按钮 , 添加 Python 编译器 ;

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_python_10

选择配置 Virtualenv Environment , 然后选择 3.7 版本的编译器 ;

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_pip_11

编辑完后 , 点击 OK 回到 之前界面 , 显示已经编辑的 编译器版本 3.7 ;

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_PyTorch_12

回到主界面 , 就可以在下拉菜单中配置 3.7 版本的编译器 ;

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_PyTorch_13

此时显示 PyTorch 软件包安装成功 ;

【错误记录】PyCharm 中安装软件包出错 ( Try to run this command from the system terminal. Make sure that you use )_python_14