Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your...模塊導入問題


Try to run this command from the system terminal. Make sure that you use the correct version of '...

 

 

 
image.png

PyCharm中導入flask這個module時遇到:

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 '/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7'. 40:490: execution error: Traceback (most recent call last): File "/Applications/PyCharm.app/Contents/helpers/packaging_tool.py", line 184, in main retcode = do_install(pkgs) File "/Applications/PyCharm.app/Contents/helpers/packaging_tool.py", line 109, in do_install return pip.main(['install'] + pkgs) AttributeError: 'module' object has no attribute 'main' (4) 
 
image.png

但是在命令窗口中通過pip install flask安裝成功,並且可以導入flask:

TQ-WXdeMacBook-Pro:~ anonyper$ pip install flask Collecting flask Using cached https://files.pythonhosted.org/packages/7f/e7/08578774ed4536d3242b14dacb4696386634607af824ea997202cd0edb4b/Flask-1.0.2-py2.py3-none-any.whl Requirement already satisfied: Jinja2>=2.10 in /usr/local/lib/python2.7/site-packages (from flask) (2.10) Requirement already satisfied: itsdangerous>=0.24 in /usr/local/lib/python2.7/site-packages (from flask) (0.24) Requirement already satisfied: Werkzeug>=0.14 in /usr/local/lib/python2.7/site-packages (from flask) (0.14.1) Requirement already satisfied: click>=5.1 in /usr/local/lib/python2.7/site-packages (from flask) (6.7) Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python2.7/site-packages (from Jinja2>=2.10->flask) (1.0) Installing collected packages: flask Successfully installed flask-1.0.2 TQ-WXdeMacBook-Pro:~ anonyper$ python Python 2.7.15 (default, Jun 17 2018, 12:46:58) [GCC 4.2.1 Compatible Apple LLVM 9.1.0 (clang-902.0.39.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import flask >>> 

可以看到是可以成功安裝並導入的,但是在PyCharm中就一直無法import,而且install的時候就會報上面的錯誤,這是因為PyCharm選擇的python解釋器和pip版本的問題,通過PyCharm的設置可以看到使用的解釋器python版本是:2.7.10版本,而在命令窗口使用python -v版本可以看到命令窗口使用的python版本是2.7.15_1

TQ-WXdeMacBook-Pro:~ anonyper$ python -v # installing zipimport hook import zipimport # builtin # installed zipimport hook # /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc matches /usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py 

而且仔細看就可以發現兩個python的路徑也不一樣,其中PyCharm中使用的是Mac 系統內置的版本,這個時候需要切換PyCharm解釋器為命令窗口使用的版本Python 2.7.15: (/usr/local/Cellar/python@2/2.7.15_1/Frameworks/Python.framework/Versions/2.7/bin/python2.7) 

 
image.png


切換的時候可能(我的是已經添加進去了)在設置中不會有這個版本的選項,這個時候只需要手動把路徑寫進去即可。

 
image.png

 

 
image.png


然后就發現flask這個module可以被導入了。

 

 
image.png


免責聲明!

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



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