首页 > 技术文章 > Try to run this command from the system terminal. Make sure that you use the correct version of 'pip' installed for your...模块导入问题

lucky-cat233 2020-03-31 15:29 原文

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

推荐阅读