简体   繁体   English

如何为Python 2.7安装mechanize?

[英]How to install mechanize for Python 2.7?

I saved mechanize in my Python 2.7 directory. 我在我的Python 2.7目录中保存了mechanize。 But when I type import mechanize into the Python shell, I get an error message that reads: 但是当我在Python shell中输入import mechanize时,我收到一条错误消息:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import mechanize
ImportError: No module named mechanize

using pip : 使用点子

pip install mechanize

or download the mechanize distribution archive, open it, and run: 或者下载mechanize distribution archive,打开它,然后运行:

python setup.py install

在Debian / Ubuntu上试试这个:

sudo apt-get install python-mechanize

You need to follow the installation instructions and not just download the files into your Python27 directory. 您需要按照安装说明进行操作 ,而不仅仅是将文件下载到Python27目录中。 It has to be installed in the site-packages directory properly, which the directions tell you how to do. 它必须正确安装在site-packages目录中,方向告诉您如何操作。

Here's what I did which worked: 这是我做的工作:

yum install python-pip
pip install -U multi-mechanize

I dont know why , but "pip install mechanize" didnt work for me . 我不知道为什么,但“pip install mechanize”对我没有用。 easy install worked anyway . 无论如何都很容易安装。 Try this : 尝试这个 :

sudo easy_install mechanize
sudo pip-2.7 install mechanize

You need the actual package (the directory containing __init__.py ) stored somewhere that's in your system's PYTHONPATH. 您需要存储在系统的PYTHONPATH中的某个实际包(包含__init__.py的目录)。 Normally, packages are distributed with a directory above the package directory, containing setup.py (which you should use to install the package), documentation, etc. This directory is not a package. 通常,包与包目录上方的目录一起分发,包含setup.py (您应该用它来安装包),文档等。这个目录不是包。 Additionally, your Python27 directory is probably not in PYTHONPATH; 另外,你的Python27目录可能不在PYTHONPATH中; more likely one or more subdirectories of it are. 更可能是它的一个或多个子目录。

You need to install the python-setuptools package: 您需要安装python-setuptools包:

apt-get install python-setuptools on Debian-ish systems yum install python-setuptools on Redhat-ish systems Debian-ish系统上的apt-get install python-setuptools yum install python-setuptools在Redhat-ish系统上yum install python-setuptools

Use sudo if applicable 如果适用,请使用sudo

install dependencies on Debian/Ubuntu: 在Debian / Ubuntu上安装依赖项:

$ sudo apt-get install python-pip python-matplotlib

install multi-mechanize from PyPI using Pip : 使用PipPyPI安装multi-mechanize:

$ sudo pip install -U multi-mechanize

It seems you need to follow the installation instructions in Daniel DiPaolo's answer to try one of the two approaches below 您似乎需要按照Daniel DiPaolo的回答中的安装说明尝试以下两种方法之一

  1. install easy_install first by running "easy_install mechanize", or 首先通过运行“easy_install mechanize”来安装easy_install,或者
  2. download the zipped package mechanize-0.2.5.tar.gz/mechanize-0.2.5.zip and (IMPORTANT) unzip the package to the directory where your .py file resides (ie "the resulting top-level directory" per the instructions). 下载压缩包mechanize-0.2.5.tar.gz / mechanize-0.2.5.zip和(重要)将包解压缩到.py文件所在的目录(即根据说明“生成的顶级目录”) )。 Then install the package by running "python setup.py install". 然后通过运行“python setup.py install”安装软件包。

Hopefully that will resolve your issue! 希望这将解决您的问题!

pip install mechanize

mechanize supports only python 2. mechanize仅支持python 2。

For python3 refer https://stackoverflow.com/a/31774959/4773973 for alternatives. 对于python3,请参阅https://stackoverflow.com/a/31774959/4773973以获取替代方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM