hiimweiwang

Problem:  Module \'pip\' have no attribute the \'main\'

之前在学习python爬虫的视频偶然看到一次讲师直接在pycharm中安装程序包,对于小白并且只知道在cmd下进行pip install  命令进行安装的我来说很是新奇。

并且,直接在pycharm中安装包好处有二:

01、在pycharm下编写python程序时,同时能在pycharm下安装所需要的包很是方便。

02、避免了电脑中安装多个python环境,使得在cmd下安装的包肯恶搞安装的不是现在说使用的环境。

 

刚开始我的操作:file>>setting>>python interpreter>>点击右上角的“+”进行安装新的包

 

然后出现下述界面

 

在不勾选Istall......的选项后在收索框里收索需要的包,然后进行Install Package。

然后问题来了,系统进行了如下报错:

 1 Executed command:
 2     pip install --index-url http://mirrors.aliyun.com/pypi/simple/ pydot
 3 
 4 Error occurred:
 5     AttributeError: module \'pip\' has no attribute \'main\'
 6 
 7 Traceback (most recent call last):
 8     File ""
 9     File ""
10         return pip.main([\'install\'+pkgs])
11 AttributeError: module \'pip\' has no attribute \'main\'
View Code

 

在网上经历了各种的收索让我找到了解决的方法和错误的额原因,原来是因为我更新了pip这个python包(pip8.0-->pip18.0)

解决的方法

在安装pycharm的文件夹下找的helpers,然后在里面找到以恶叫packing_tools.py的文件,然后打开

找到如下代码:

 1 def do_install(pkgs):
 2     try:
 3         import pip
 4     except ImortError:
 5         error_no_pip()
 6     return pip.main([\'install\']+pkgs)
 7 
 8 def do_uninstall(pkgs):
 9     try:
10         import pip
11     except ImortError:
12         error_no_pip()
13     return pip.main([\'uninstall\']+pkgs)
View Code

将其修改为如下的代码,然后进行保存即可:

 1 def do_install(pkgs):
 2     try:   
 3         try:
 4             from pip._internal import main
 5         except Exception:
 6                 from pip import main
 7     except ImportError:
 8         error_no_pip()
 9     return main([\'install\']+pkgs)
10 
11 def do_install(pkgs):
12     try:   
13         try:
14             from pip._internal import main
15         except Exception:
16                 from pip import main
17     except ImportError:
18         error_no_pip()
19     return main([\'uninstall\']+pkgs)
View Code

分类:

技术点:

相关文章: