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

Pythonモジュールを使用pipまたはインストールできます。easy_install

 $ 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

おすすめ記事