编程语言
首页 > 编程语言> > software-installation – 如何正确安装Python包?

software-installation – 如何正确安装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似乎是praw的依赖:https://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

解决方法:

您可以使用pip或easy_install来安装python模块.

 $pip install <package-name>

编辑:

我尝试安装urllib2包,它告诉我,urllib2的实名是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
>>> 

EDIT2:

你可以从源代码安装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

标签:python,slackware,software-installation
来源: https://codeday.me/bug/20190814/1655828.html