如何正确安装Python包?

如何正确安装Python包?

我运行 Slackware 系统,并尝试运行一些 Python 代码,但出现很多错误,如下所示:

>>> import urllib2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.6/urllib2.py", line 91, in <module>
    import hashlib
  File "/usr/lib/python2.6/hashlib.py", line 136, in <module>
    md5 = __get_builtin_constructor('md5')
  File "/usr/lib/python2.6/hashlib.py", line 63, in __get_builtin_constructor
    import _md5
ImportError: No module named _md5

urllib2 应该是 Python 的一个相当基本的库,我怎样才能让 Python 正常工作?

原因是这urllib2似乎依赖于prawhttps://github.com/praw-dev/praw/issues/135

尝试安装 pip 来安装它会给我:

Traceback (most recent call last):
  File "setup.py", line 5, in <module>
    from setuptools import setup
ImportError: No module named setuptools

所以我尝试安装setuptools:

running install
Traceback (most recent call last):
  File "setup.py", line 94, in <module>
    scripts = scripts,
  File "/usr/lib/python2.6/distutils/core.py", line 152, in setup
    dist.run_commands()
  File "/usr/lib/python2.6/distutils/dist.py", line 975, in run_commands
    self.run_command(cmd)
  File "/usr/lib/python2.6/distutils/dist.py", line 995, in run_command
    cmd_obj.run()
  File "/root/setuptools-0.6c11/setuptools/command/install.py", line 76, in run
    self.do_egg_install()
  File "/root/setuptools-0.6c11/setuptools/command/install.py", line 85, in do_egg_install
    easy_install = self.distribution.get_command_class('easy_install')
  File "/root/setuptools-0.6c11/setuptools/dist.py", line 395, in get_command_class
    self.cmdclass[command] = cmdclass = ep.load()
  File "/root/setuptools-0.6c11/pkg_resources.py", line 1954, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/root/setuptools-0.6c11/setuptools/command/easy_install.py", line 21, in <module>
    from setuptools.package_index import PackageIndex, parse_bdist_wininst
  File "/root/setuptools-0.6c11/setuptools/package_index.py", line 2, in <module>
    import sys, os.path, re, urlparse, urllib2, shutil, random, socket, cStringIO
  File "/usr/lib/python2.6/urllib2.py", line 91, in <module>
    import hashlib
  File "/usr/lib/python2.6/hashlib.py", line 136, in <module>
    md5 = __get_builtin_constructor('md5')
  File "/usr/lib/python2.6/hashlib.py", line 63, in __get_builtin_constructor
    import _md5
ImportError: No module named _md5

答案1

您可以使用pipeasy_install安装 python 模块。

 $ pip install <package-name>

编辑:

我尝试安装urllib2软件包,它告诉我real name of requirement urllib2 is urllib3.这是发生的事情:

pradeep@pradeep-laptop:~$ sudo pip install urllib2
Downloading/unpacking urllib2
  Real name of requirement urllib2 is urllib3
  Could not find any downloads that satisfy the requirement urllib2
No distributions at all found for urllib2
Storing complete log in /home/pradeep/.pip/pip.log
pradeep@pradeep-laptop:~$ sudo pip install urllib3
Downloading/unpacking urllib3
  Downloading urllib3-1.5.tar.gz
  Running setup.py egg_info for package urllib3

Installing collected packages: urllib3
  Running setup.py install for urllib3

Successfully installed urllib3
Cleaning up...
pradeep@pradeep-laptop:~$ python
Python 2.7.3 (default, Apr 20 2012, 22:39:59) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib3
>>> 

编辑2:

您可以从源代码安装 python-pip。

$ wget http://pypi.python.org/packages/source/p/pip/pip-0.7.2.tar.gz
$ tar xzf pip-0.7.2.tar.gz
$ cd pip-0.7.2
$ python setup.py install

答案2

Pip 是安装 Python 模块的正确方法。 Easy_installer 是默认的 python 软件包安装程序,但 pip 取代了它,并且通常应该是首选,尽管少数软件包只能使用 easy_installer 安装。

其他答案无法解决的地方是如何在 Slackware 机器上安装软件。您应该查看 Slackbuilds.org,而不是自己构建新软件。该存储库共享软件源的链接,以及 Slackware 的自定义构建脚本,用于编译软件并制作可以轻松安装/卸载/升级的 Slackware 软件包,并由 Slackware 的软件包管理系统 installpkg/updatepkg/removepkg 跟踪。 Slackbuilds 还会让您了解对其他 slackbuilds 软件包的依赖关系。请注意,slackbuilds 假设有一个全面的 Slackware 安装,并且不会告诉您对标准 slack 软件包的任何依赖关系。

假设 Slack 14.0,您将需要: http://slackbuilds.org/repository/14.0/python/pip/ http://slackbuilds.org/repository/14.0/python/distribute/

最后,请研究 slackpkg 和 sbopkg,以获得更全面的 Slackware 自动化包管理。 Slackware 官方不支持 slackpkg 或 slackbuilds,尽管它们被广泛使用且维护良好。

相关内容