Python 导入错误:没有名为 datetime 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26031160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:56:44  来源:igfitidea点击:

ImportError: No module named datetime

pythonubuntu-12.04ubuntu-14.04

提问by akbsmile

When I upgrade my Ubuntu into 14.04 from 12.04, this time I get this error:

当我将 Ubuntu 从 12.04 升级到 14.04 时,这次我收到此错误:

 ImportError: No module named datetime

采纳答案by erjiang

This happened to me when I created a virtualenv and then upgraded from 12.04 to 14.04.

当我创建一个 virtualenv 然后从 12.04 升级到 14.04 时,这发生在我身上。

I had to delete my virtualenv and recreate it, and after doing that, everything worked again.

我不得不删除我的 virtualenv 并重新创建它,然后,一切又恢复了。

回答by akbsmile

I tried to re-install it by these steps.

我尝试通过这些步骤重新安装它。

1. remove the old version of venv
2. virtualenv venv
3. .venv/activate/bin
4. pip install -r requirements/ requirements.txt

And it works perfectly. Thanks guys :)

它完美地工作。谢谢你们 :)

回答by han058

Try...

尝试...

>>> import sys
>>> sys.path
[... '/usr/local/lib/python2.7/lib-dynload',...]

if lib-dynload not included in sys.path, You could not import datetime

如果 sys.path 中未包含 lib-dynload,则不能 import datetime

check it!

核实!

回答by artisare

Just run this command. It worked like a charm!

只需运行此命令。它就像一个魅力!

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

This just happened to me after the 14.10 update, and it seems to be because my virtual environments have old copies of /usr/bin/python2.7that — unlike the new binary — do not include datetimebuilt-in, and so get an error when they cannot find it on disk anywhere. The new interpreter seems to import it without any file I/O (try running it under strace to check).

这只是在 14.10 更新后发生在我身上,似乎是因为我的虚拟环境有旧的副本/usr/bin/python2.7——不像新的二进制文件——不包括datetime内置的,所以当他们在磁盘上的任何地方都找不到它时会出错. 新的解释器似乎在没有任何文件 I/O 的情况下导入它(尝试在 strace 下运行它以进行检查)。

More info here

更多信息在这里

回答by Seg-mel

If you use virtualenv, updating it might solve this issue

如果您使用virtualenv,更新它可能会解决此问题

virtualenv /path/to/old/virtualenv/

回答by Hasan Ramezani

just reinitialize the virtualenv by:

只需通过以下方式重新初始化 virtualenv:

cd <virtualenv-dir>
virtualenv .

回答by Walter B

Same happened to me on upgrading Ubuntu from 14.04 to 15.10.

在将 Ubuntu 从 14.04 升级到 15.10 时,我也遇到了同样的情况。

I solved it by upgrading pip and then removing and recreating the virtual env:

我通过升级 pip 然后删除并重新创建虚拟环境来解决它:

$ easy_install --upgrade pip
$ rmvirtualenv <my_virtual_env>
$ mkvirtualenv <my_virtual_env>

(I use virtualenvwrapper)

(我使用 virtualenvwrapper)

回答by Aneesh R S

  1. clear virtual env, consider VIRTUAL is the name of the virtual environment

    virtualenv --clear VIRTUAL

  2. Now activate it and install from requirements.txt

    source VIRTUAL/bin/activate pip install -r requirements.txt

  1. 清除虚拟环境,考虑 VIRTUAL 是虚拟环境的名称

    virtualenv --clear 虚拟

  2. 现在激活它并从 requirements.txt 安装

    source VIRTUAL/bin/activate pip install -r requirements.txt

回答by dug

If by chance you come across this error while trying to renew your LetsEncrypt certificate (like I did) I found the solution here:

如果您在尝试更新 LetsEncrypt 证书时偶然遇到此错误(就像我所做的那样),我在这里找到了解决方案:

https://askubuntu.com/a/850669/668101

https://askubuntu.com/a/850669/668101

Remove this folder and rerun LetsEncrypt and it will recreate all the relevant files and avoid the error from this thread.

删除此文件夹并重新运行 LetsEncrypt,它将重新创建所有相关文件并避免此线程中的错误。

rm ~/.local/share/letsencrypt -R

回答by Artur Samigullin

If you face datetime import issue using IntelliJ PyCharm or Idea and from Console/Terminal it works fine, you should just duplicate/recreate running configurations.

如果您使用 IntelliJ PyCharm 或 Idea 遇到日期时间导入问题并且从控制台/终端它工作正常,您应该只复制/重新创建运行配置。