在安装mechanize后,我似乎无法导入它。
我已经尝试从pip、easy_install和通过python setup.py从这个repo安装:https://github.com/abielr/mechanize。所有这些都无济于事,因为每次我输入Python交互时,我得到:
Python 2.7.3 (default, Aug 1 2012, 05:14:39)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mechanize
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mechanize
>>>
我之前运行的安装报告已经成功完成,因此我希望导入能够正常工作。是什么导致了这个错误?
在我的例子中,我运行pip install Django==1.11,它不会从python解释器中导入。
浏览pip的命令,我发现pip show是这样的:
> pip show Django
Name: Django
Version: 1.11
...
Location: /usr/lib/python3.4/site-packages
...
注意位置显示的是'3.4'。我发现python-命令被链接到python2.7
/usr/bin> ls -l python
lrwxrwxrwx 1 root root 9 Mar 14 15:48 python -> python2.7
就在它旁边,我发现了一个名为python3的链接,所以我使用了它。还可以将链接更改为python3.4。这也能解决问题。
当您通过easy_install或pip安装时,它是否成功完成?全输出是多少?您正在使用哪个python安装?如果您正在将模块安装到系统目录(如果您正在使用系统python安装),则可能需要在使用安装命令之前使用sudo。你的问题中没有太多有用的信息,但是一些工具可能会有帮助,包括:
echo $PYTHONPATH and/or echo $PATH: when importing modules, Python searches one of these environment variables (lists of directories, : delimited) for the module you want. Importing problems are often due to the right directory being absent from these lists
which python, which pip, or which easy_install: these will tell you the location of each executable. It may help to know.
Use virtualenv, like @JesseBriggs suggests. It works very well with pip to help you isolate and manage the modules and environment for separate Python projects.