Python:没有名为 datetime 的模块?

Python:没有名为 datetime 的模块?

操作系统:Ubuntu 14.04 LTS

Python:2.7.6

我安装的 Gourmet Recipe Manager 突然停止加载。当我在终端窗口中运行它时,我在回溯结束时收到以下内容:

import datetime as dt
ImportError: No module named datetime

据我所知,什么都没有改变,我的 Python 安装是最新的。它昨天停止工作了。我当然希望有一个好的方法来诊断和解决这个问题!

更新:感谢所有回复的人!

Tim,如果我问错了地方,我很抱歉。请把它当作新手,只需点击 Ubuntu 网站上的链接即可。

TheSchwa,我尝试了您的建议,但收到了与上述相同的错误消息。

muru,该软件包似乎已安装,但我不知道它是否已正确安装/配置。我该如何查明?

很抱歉问了这么多问题,但我是 RedHat 的老用户,已经远离 Linux 一段时间了。apt/dpkg 的所有东西对我来说都是新的。

答案1

14.10 更新后,我才遇到这种情况,似乎是因为我的虚拟环境有旧副本/usr/bin/python2.7(与新二进制文件不同),不包括datetime内置文件,因此在磁盘上找不到它时会出错。新解释器似乎无需任何文件 I/O 即可导入它(尝试在下面运行它strace进行检查)。

我通过激活并运行来修复每个虚拟环境:

$ cp /usr/bin/python2.7 $(which python2.7)

答案2

您可以通过以下方式重新初始化虚拟环境:

cd $VIRTUAL_ENV
virtualenv .

答案3

如果你在升级后尝试使用 letsencrypt 时遇到此问题,此解决方案对我有用letsencrypt 论坛 - 无法使用自动导入:错误:没有名为 io/2345/3 的模块

我只需要删除这个:

rm ~/.local/share/letsencrypt -R

答案4

由于我发现 14.04 中存在一些变化,因此您需要从 root 身份执行以下操作:

仅适用于日期时间:


ln -s /usr/lib/python2.7/lib-dynload/datetime.x86_64-linux-gnu.so                      /usr/lib/python2.7/lib-dynload/datetime.so

对于所有模块:


ln -s /usr/lib/python2.7/lib-dynload/audioop.x86_64-linux-gnu.so                       /usr/lib/python2.7/lib-dynload/audioop.so
ln -s /usr/lib/python2.7/lib-dynload/_bsddb.x86_64-linux-gnu.so                        /usr/lib/python2.7/lib-dynload/_bsddb.so
ln -s /usr/lib/python2.7/lib-dynload/bz2.x86_64-linux-gnu.so                           /usr/lib/python2.7/lib-dynload/bz2.so
ln -s /usr/lib/python2.7/lib-dynload/_codecs_cn.x86_64-linux-gnu.so                    /usr/lib/python2.7/lib-dynload/_codecs_cn.so
ln -s /usr/lib/python2.7/lib-dynload/_codecs_hk.x86_64-linux-gnu.so                    /usr/lib/python2.7/lib-dynload/_codecs_hk.so
ln -s /usr/lib/python2.7/lib-dynload/_codecs_iso2022.x86_64-linux-gnu.so               /usr/lib/python2.7/lib-dynload/_codecs_iso2022.so
ln -s /usr/lib/python2.7/lib-dynload/_codecs_jp.x86_64-linux-gnu.so                    /usr/lib/python2.7/lib-dynload/_codecs_jp.so
ln -s /usr/lib/python2.7/lib-dynload/_codecs_kr.x86_64-linux-gnu.so                    /usr/lib/python2.7/lib-dynload/_codecs_kr.so
ln -s /usr/lib/python2.7/lib-dynload/_codecs_tw.x86_64-linux-gnu.so                    /usr/lib/python2.7/lib-dynload/_codecs_tw.so
ln -s /usr/lib/python2.7/lib-dynload/crypt.x86_64-linux-gnu.so                         /usr/lib/python2.7/lib-dynload/crypt.so
ln -s /usr/lib/python2.7/lib-dynload/_csv.x86_64-linux-gnu.so                          /usr/lib/python2.7/lib-dynload/_csv.so
ln -s /usr/lib/python2.7/lib-dynload/_ctypes_test.x86_64-linux-gnu.so                  /usr/lib/python2.7/lib-dynload/_ctypes_test.so
ln -s /usr/lib/python2.7/lib-dynload/_ctypes.x86_64-linux-gnu.so                       /usr/lib/python2.7/lib-dynload/_ctypes.so
ln -s /usr/lib/python2.7/lib-dynload/_curses_panel.x86_64-linux-gnu.so                 /usr/lib/python2.7/lib-dynload/_curses_panel.so
ln -s /usr/lib/python2.7/lib-dynload/_curses.x86_64-linux-gnu.so                       /usr/lib/python2.7/lib-dynload/_curses.so
ln -s /usr/lib/python2.7/lib-dynload/datetime.x86_64-linux-gnu.so                      /usr/lib/python2.7/lib-dynload/datetime.so
ln -s /usr/lib/python2.7/lib-dynload/dbm.x86_64-linux-gnu.so                           /usr/lib/python2.7/lib-dynload/dbm.so
ln -s /usr/lib/python2.7/lib-dynload/_elementtree.x86_64-linux-gnu.so                  /usr/lib/python2.7/lib-dynload/_elementtree.so
ln -s /usr/lib/python2.7/lib-dynload/fpectl.x86_64-linux-gnu.so                        /usr/lib/python2.7/lib-dynload/fpectl.so
ln -s /usr/lib/python2.7/lib-dynload/future_builtins.x86_64-linux-gnu.so               /usr/lib/python2.7/lib-dynload/future_builtins.so
ln -s /usr/lib/python2.7/lib-dynload/_hashlib.x86_64-linux-gnu.so                      /usr/lib/python2.7/lib-dynload/_hashlib.so
ln -s /usr/lib/python2.7/lib-dynload/_hotshot.x86_64-linux-gnu.so                      /usr/lib/python2.7/lib-dynload/_hotshot.so
ln -s /usr/lib/python2.7/lib-dynload/_json.x86_64-linux-gnu.so                         /usr/lib/python2.7/lib-dynload/_json.so
ln -s /usr/lib/python2.7/lib-dynload/linuxaudiodev.x86_64-linux-gnu.so                 /usr/lib/python2.7/lib-dynload/linuxaudiodev.so
ln -s /usr/lib/python2.7/lib-dynload/_lsprof.x86_64-linux-gnu.so                       /usr/lib/python2.7/lib-dynload/_lsprof.so
ln -s /usr/lib/python2.7/lib-dynload/mmap.x86_64-linux-gnu.so                          /usr/lib/python2.7/lib-dynload/mmap.so
ln -s /usr/lib/python2.7/lib-dynload/_multibytecodec.x86_64-linux-gnu.so               /usr/lib/python2.7/lib-dynload/_multibytecodec.so
ln -s /usr/lib/python2.7/lib-dynload/_multiprocessing.x86_64-linux-gnu.so              /usr/lib/python2.7/lib-dynload/_multiprocessing.so
ln -s /usr/lib/python2.7/lib-dynload/nis.x86_64-linux-gnu.so                           /usr/lib/python2.7/lib-dynload/nis.so
ln -s /usr/lib/python2.7/lib-dynload/ossaudiodev.x86_64-linux-gnu.so                   /usr/lib/python2.7/lib-dynload/ossaudiodev.so
ln -s /usr/lib/python2.7/lib-dynload/parser.x86_64-linux-gnu.so                        /usr/lib/python2.7/lib-dynload/parser.so
ln -s /usr/lib/python2.7/lib-dynload/pyexpat.x86_64-linux-gnu.so                       /usr/lib/python2.7/lib-dynload/pyexpat.so
ln -s /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so                      /usr/lib/python2.7/lib-dynload/readline.so
ln -s /usr/lib/python2.7/lib-dynload/resource.x86_64-linux-gnu.so                      /usr/lib/python2.7/lib-dynload/resource.so
ln -s /usr/lib/python2.7/lib-dynload/_sqlite3.x86_64-linux-gnu.so                      /usr/lib/python2.7/lib-dynload/_sqlite3.so
ln -s /usr/lib/python2.7/lib-dynload/_ssl.x86_64-linux-gnu.so                          /usr/lib/python2.7/lib-dynload/_ssl.so
ln -s /usr/lib/python2.7/lib-dynload/termios.x86_64-linux-gnu.so                       /usr/lib/python2.7/lib-dynload/termios.so
ln -s /usr/lib/python2.7/lib-dynload/_testcapi.x86_64-linux-gnu.so                     /usr/lib/python2.7/lib-dynload/_testcapi.so

此外如果你使用虚拟环境复制

cp $(which python2.7) /opt/graphite/bin/python

到你的环境。

相关内容