Trouble importing module in Python 2.7

和自甴很熟 提交于 2020-01-06 07:20:50

问题


My Mac computer has Python 3.6 and Python 2.7 and I have successfully installed the basic modules such as numpy, scipy and matplotlib, for example, by doing the routine pip install and pip3 install. My Python 3.6 works totally well in Anaconda-Jupiter-Notebook, IDLE and Terminal, while Python 2.7 works only in terminal but not in IDLE.

Then for version check I tried

pip --version

Returning: pip 10.0.1 from /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip (python 2.7)

pip3 --version

Returning: pip 10.0.1 from /Users/son520804/anaconda3/lib/python3.6/site-packages/pip (python 3.6)

Then,

which python2

Returning: /Library/Frameworks/Python.framework/Versions/2.7/bin/python2

which pip2

Returning: /Library/Frameworks/Python.framework/Versions/2.7/bin/pip2

How could I resolve this issue and enable the Python 2.7 idle to import the modules? Much appreciated for your help.


回答1:


find / -iname '*numpy*'

This is a terminal operation not a python command, you can try running that in the terminal to see where it's storing numpy, but you'll probably get the python3's version.

Try:

pip2 uninstall numpy

Then:

pip2 install numpy

It might be due to your machine seeing python3 as your default "python" so pip might actually be installing it again to python3. By denoting pip2 it should be linked to python2 (might actually need to do 'pip2.7'). Hope it helps!




回答2:


Perhaps your pip is an alias to pip3. Find it out by pip --version and if so install packages for python 2.7 as pip2.7 install matplotlib



来源:https://stackoverflow.com/questions/50691683/trouble-importing-module-in-python-2-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!