Python脚本未执行(找不到模块APNSWrapper)

0 投票
1 回答
1208 浏览
提问于 2025-04-18 10:20

在终端里,我运行了这个命令:

Python pnot.py

I get the following error:

Traceback (most recent call last):
  File "pnot.py", line 1, in <module>
    from APNSWrapper import *
ImportError: No module named APNSWrapper

我尝试安装这个模块:

pip install APNSWrapper==0.6.1

Requirement already satisfied (use --upgrade to upgrade): APNSWrapper==0.6.1 in /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages
Requirement already satisfied (use --upgrade to upgrade): docutils>=0.3 in /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages (from APNSWrapper==0.6.1)
Cleaning up...

我也尝试安装APNS:

Pip install apns 

但是我遇到了以下错误:

Downloading/unpacking apns
  Downloading apns-1.1.2.tar.gz
  Running setup.py egg_info for package apns

Installing collected packages: apns
  Running setup.py install for apns
    error: /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/__pycache__/apns.cpython-33.pyc: Permission denied
    Complete output from command /Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3 -c "import setuptools;__file__='/private/var/folders/8g/6w73vzqs04b6f1cq8m8pyswc0000gn/T/pip_build_samiesyed/apns/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/8g/6w73vzqs04b6f1cq8m8pyswc0000gn/T/pip-3yfn_n-record/install-record.txt --single-version-externally-managed:
    running install

running build

running build_py

creating build

creating build/lib

copying apns.py -> build/lib

running install_lib

copying build/lib/apns.py -> /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages

byte-compiling /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/apns.py to apns.cpython-33.pyc

error: /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/site-packages/__pycache__/apns.cpython-33.pyc: Permission denied

----------------------------------------
Cleaning up...
Command /Library/Frameworks/Python.framework/Versions/3.3/bin/python3.3 -c "import setuptools;__file__='/private/var/folders/8g/6w73vzqs04b6f1cq8m8pyswc0000gn/T/pip_build_samiesyed/apns/setup.py';exec(compile(open(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /var/folders/8g/6w73vzqs04b6f1cq8m8pyswc0000gn/T/pip-3yfn_n-record/install-record.txt --single-version-externally-managed failed with error code 1 in /private/var/folders/8g/6w73vzqs04b6f1cq8m8pyswc0000gn/T/pip_build_s/apns
Storing complete log in /Users/s/.pip/pip.log

脚本代码:

from APNSWrapper import *

wrapper = APNSNotificationWrapper('cert.pem', True)
for token in ['xxxxxxx']:
    token = binascii.unhexlify(token)
    apn = APNSNotification()
    apn.token(token)
    alert = APNSAlert()
    alert.body('hello world')
    apn.alert(alert)
    apn.sound()
    wrapper.append(apn)
wrapper.notify()

这让我非常沮丧,不知道为什么这个脚本没有执行。

1 个回答

1

很可能是 pippython 指向了不同的 Python 安装版本。一个可能是通过软件包管理器安装的,另一个可能是系统默认的版本。

你可以通过以下命令来确认这一点:

which python

which pip

很可能 pip 安装的包是针对你正在尝试使用的另一个 Python 安装版本。

无论问题表现得如何,解决这个问题的办法是使用 virtualenv 环境来安装你的 Python 包。virtualenv 会创建一个独立的文件夹,里面包含 Python 解释器和安装的包,这样如果出现问题,你可以轻松地清空并重新构建这个环境。

首先安装 virtualenv

然后执行:

 virtualenv venv   # Create virtualenv installation in folder called venv
 source venv/bin/activate  # Modify your shell and PATH to use python from venv/bin/python
 pip install apns  # Installs apns in venv/lib
 python pnot.py   # Now it runs your script using venv/bin/python interpreter 
 # and packages installed in venv/lib/python2.7

关于 Python 和 virtualenv 安装的更多信息:

撰写回答