Software-Installation

如何正確安裝 Python 包?

  • January 31, 2013

我執行一個 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

您可以使用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

引用自:https://unix.stackexchange.com/questions/53352