python - pytz 在 Mac 上安装

标签 python macos pip pytz

Django 提示“ImportError: No module named pytz”,但是当我使用 pip 安装它时,我得到了这个结果:

Requirement already satisfied (use --upgrade to upgrade): pytz in /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python

它是否有可能在错误的位置查找,或者我需要尝试将它安装到其他地方?

这是完整的堆栈跟踪:

Internal Server Error: /basicloginwebservice/
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/django/core/handlers/base.py", line 103, in get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 321, in resolve
    sub_match = pattern.resolve(new_path)
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 223, in resolve
    return ResolverMatch(self.callback, args, kwargs, self.name)
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 230, in callback
    self._callback = get_callable(self._callback_str)
  File "/Library/Python/2.7/site-packages/django/utils/functional.py", line 29, in wrapper
    result = func(*args)
  File "/Library/Python/2.7/site-packages/django/core/urlresolvers.py", line 97, in get_callable
    mod = import_module(mod_name)
  File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/<redacted>/django/<redacted>/<redacted>/views.py", line 3, in <module>
    import pytz
ImportError: No module named pytz

谢谢

最佳答案

/System/Library/Frameworks/Python.framework/Versions/2.7/Extras 是与 Apple 提供的 Python 2.7 (/usr/bin/python2.7)。不可能从回溯中分辨出来,但 Django 有可能在不同的 Python 2.7 实例下运行,可能链接到 /usr/local/bin。当存在多个 Python 实例并且您直接从命令行使用 pip 时,可能会出现此类问题。确保为您使用的每个 Python 安装了一个版本的 pip。而且,为确保您使用的是正确的 pip 实例,您可以这样调用它:

python -m pip install pytz

python 替换为用于运行 Django 的相同路径。

另一种方法是始终使用激活的 virtualenv,这应该确保首先在进程中找到正确的 pythonpip 实例 路径

关于python - pytz 在 Mac 上安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30224539/

相关文章:

Python-unittest 尝试调用导入的自定义argparser

macos - jEdit Mac OS 键盘行为

regex - Mac OSX、Bash、awk 和负面回顾

尝试在 MAC 上克隆 GIT 时出现 javax.net.ssl.SSLHandshakeException

python - 定义正确的requirements.txt文件

python - 如何为墙壁添加边界?

python - 二值化数组

python:在shell上显示耗时

python - pip 安装了什么。 ( pip )是什么意思?

python - 适用于python3.7的 pip (Ubuntu 16.04)