在安装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
>>> 

我之前运行的安装报告已经成功完成,因此我希望导入能够正常工作。是什么导致了这个错误?


当前回答

也许有点跑题了,但我在导入PyYAML时遇到了问题。指出你需要导入yaml。(我猜是经典的rtfm…)

其他回答

我已经解决了我的问题,相同的库在一个项目(A)中工作良好,但在另一个项目(B)中导入这些相同的库会导致错误。我使用Pycharm作为IDE在Windows操作系统。 所以,在尝试了许多潜在的解决方案,但都未能解决问题后,我做了以下两件事(删除“Venv”文件夹,并重新配置解释器):

在项目(B)中,有一个名为(“venv”)的文件夹,位于外部库/。我删除了那个文件夹。

2-Step 1 (deleting "venv" folder) causes error in Python Interpreter Configuration, and there is a message shown at top of screen saying "Invalid python interpreter selected for the project" and "configure python interpreter", select that link and it opens a new window. There in "Project Interpreter" drop-down list, there is a Red colored line showing previous invalid interpreter. Now, Open this list and select the Python Interpreter(in my case, it is Python 3.7). Press "Apply" and "OK" at the bottom and you are good to go.

注意:这可能是我的项目(B)的虚拟环境无法识别已经安装和工作的库的问题。

在我的情况下,这是模块中缺少init.py文件的问题,我想在Python 2.7环境中导入该文件。

Python 3.3+具有隐式命名空间包,允许它在没有init.py文件的情况下创建包。

在我的例子中,我运行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。这也能解决问题。

检查您在IDE或代码编辑器的解释器和系统上使用的python版本是否相同。 例如,在终端中使用python3——version检查python版本 并在VSCode中通过cmd+shift+p->检查解释器的python版本:Select interpreter ->选择与您在终端中看到的相同的版本。

我知道这是一个超级老的帖子,但对我来说,我有一个32位python和64位python安装的问题。一旦我卸载了32位的python,一切都正常工作了。