pytz包:即使通常使用python3,也只能与python2一起运行(在pip安装pytz之后)

2024-03-28 09:44:02 发布

您现在位置:Python中文网/ 问答频道 /正文

在安装pytz以用于Python项目时,我在wsl终端上获得以下终端输出:

pip install pytz
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. A future version of pip will drop support for Python 2.7. 
More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support
Defaulting to user installation because normal site-packages is not writeable
Collecting pytz
  Downloading pytz-2019.3-py2.py3-none-any.whl (509 kB)
     |████████████████████████████████| 509 kB 1.3 MB/s
Installing collected packages: pytz
Successfully installed pytz-2019.3

之后,在运行test.py时,我仍然得到错误:

Traceback (most recent call last):   
  File "test.py", line 2, in <module>
    import pytz
ModuleNotFoundError: No module named 'pytz'

尝试import pytz-2019.3pytz2019.3会抛出语法错误

最后,我可以运行文件:python2 test.py

我想知道(对于这个软件包和其他产生类似问题的软件包)如何使用python3 test.py正常运行它。我需要更改安装中的任何内容吗

仅供参考,以下是我想要使用的代码:

from datetime import datetime
import pytz
tz_NY = pytz.timezone('America/New_York') 
datetime_NY = datetime.now(tz_NY)
print("NY time:", datetime_NY.strftime("%H:%M:%S"))

谢谢你的帮助


Tags: pipofinpytestimport终端support