在Mac上安装pytz

Cas*_*ins 2 python macos pip pytz

Django通知我"ImportError:没有名为pytz的模块",但是当我使用pip来安装它时,我得到了这个结果:

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

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

这是完整的堆栈跟踪:

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
Run Code Online (Sandbox Code Playgroud)

谢谢

Ned*_*ily 7

/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直接从命令行使用时,可能会出现这些类型的问题.确保pip为您使用的每个Python安装了一个版本.并且,为了确保您使用正确的实例pip,您可以这样调用它:

python -m pip install pytz

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

另一种方法是始终使用激活virtualenv,这应该确保在进程中首先找到权限pythonpip实例PATH.