ImportError: No module named datetime

余生颓废 提交于 2019-12-03 01:01:39

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

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

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.7 that — unlike the new binary — do not include datetime built-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).

More info here

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 :)

just reinitialize the virtualenv by:

cd <virtualenv-dir>
virtualenv .
dug

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

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.

rm ~/.local/share/letsencrypt -R

If you use virtualenv, updating it might solve this issue

virtualenv /path/to/old/virtualenv/

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

check it!

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

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

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

(I use virtualenvwrapper)

  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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!