消费脚本
代码语言:javascript复制[root@h102 python]# cat c.py
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(
host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='mq_learning_q')
print ' [*] Waiting for messages. To exit press CTRL C'
def callback(ch, method, properties, body):
print " [x] Received %r" % (body,)
channel.basic_consume(callback,
queue='mq_learning_q',
no_ack=True)
channel.start_consuming()
[root@h102 python]#
运行生产脚本
代码语言:javascript复制[root@h102 python]# python p.py
Traceback (most recent call last):
File "p.py", line 2, in <module>
import pika
ImportError: No module named pika
[root@h102 python]#
报错:缺少 pika 模块
解决办法,安装相应的包来解决依赖,建议使用pip,比较方便
代码语言:javascript复制[root@h102 python]# pip install pika
-bash: pip: command not found
[root@h102 python]# yum install python-pip
Loaded plugins: dellsysid, fastestmirror, refresh-packagekit, security
Setting up Install Process
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: ftp.riken.jp
* extras: mirrors.aliyun.com
* updates: mirrors.163.com
Resolving Dependencies
--> Running transaction check
---> Package python-pip.noarch 0:7.1.0-1.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
======================================================================================================================================
Package Arch Version Repository Size
======================================================================================================================================
Installing:
python-pip noarch 7.1.0-1.el6 epel 1.5 M
Transaction Summary
======================================================================================================================================
Install 1 Package(s)
Total download size: 1.5 M
Installed size: 6.6 M
Is this ok [y/N]: y
Downloading Packages:
python-pip-7.1.0-1.el6.noarch.rpm | 1.5 MB 00:31
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
Warning: RPMDB altered outside of yum.
** Found 6 pre-existing rpmdb problem(s), 'yum check' output follows:
perl-DBD-MySQL-4.013-3.el6.x86_64 has missing requires of libmysqlclient.so.16()(64bit)
perl-DBD-MySQL-4.013-3.el6.x86_64 has missing requires of libmysqlclient.so.16(libmysqlclient_16)(64bit)
ruby-mysql-2.8.2-1.el6.x86_64 has missing requires of libmysqlclient.so.16()(64bit)
ruby-mysql-2.8.2-1.el6.x86_64 has missing requires of libmysqlclient.so.16(libmysqlclient_16)(64bit)
ruby193-rubygem-mysql2-0.3.11-4.el6.x86_64 has missing requires of libmysqlclient_r.so.16()(64bit)
ruby193-rubygem-mysql2-0.3.11-4.el6.x86_64 has missing requires of libmysqlclient_r.so.16(libmysqlclient_16)(64bit)
Installing : python-pip-7.1.0-1.el6.noarch 1/1
Verifying : python-pip-7.1.0-1.el6.noarch 1/1
Installed:
python-pip.noarch 0:7.1.0-1.el6
Complete!
[root@h102 python]# pip install pika
/usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
You are using pip version 7.1.0, however version 7.1.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Collecting pika
/usr/lib/python2.6/site-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:90: InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. For more information, see https://urllib3.readthedocs.org/en/latest/security.html#insecureplatformwarning.
InsecurePlatformWarning
Downloading pika-0.10.0-py2.py3-none-any.whl (92kB)
100% |████████████████████████████████| 94kB 252kB/s
Installing collected packages: pika
Successfully installed pika-0.10.0
[root@h102 python]# echo $?
0
[root@h102 python]#