python01:pycharm中安裝包時所遇到的問題


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM