ImportError: No module named datetime
Asked Answered
C

11

40

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

 ImportError: No module named datetime
Cranio answered 25/9, 2014 at 5:25 Comment(3)
you get this error while doing what ?Yehudi
Yes, while I'm upgrading it and also after it finish upgrading, the error is still there.Cranio
the version I'm using is python 2.7.6Cranio
B
61

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.

Boole answered 22/10, 2014 at 2:38 Comment(3)
I had the same problem after I did apt-get dist-upgrade my Debian. Solution was to delete the virtualenv and recreate it.Featherbedding
Thanks for the hint! Simply running mkvirtualenv $my_venv_name solved it for me.Starling
same happened for me after upgrading from 14.04 to 16.04Nadbus
S
40

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

Supplant answered 1/4, 2015 at 7:52 Comment(2)
I took this a step further, "rm venv/bin/python*", then reinitialize with "virtualenv venv". Less destructive compared to rebuilding the whole venv.Evolution
You deserve a coffee! Thanks :) - Also caused by upgrading underlying Ubuntu 14.04 to Ubuntu 16.04.1 without touching the virtual environment.Subsellium
C
10

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

Cranio answered 25/9, 2014 at 5:53 Comment(0)
C
10

just reinitialize the virtualenv by:

cd <virtualenv-dir>
virtualenv .
Coaler answered 18/5, 2015 at 11:43 Comment(1)
This worked for me with one change. 'virtualenv .' gave me the error: 'IOError: [Errno 40] Too many levels of symbolic links' so I did: 'rm bin/python*' and then 'virtualenv .' succeeded.Yellowish
B
8

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
Bellamy answered 21/3, 2017 at 19:18 Comment(1)
Good tip! Thanks for the info!! The only thing is that the term "thread" doesn't really apply to StackOverflow as this is not a discussion forum, but a Q&A site :) Otherwise, this was really helpful! Thanks!Bunin
C
6

If you use virtualenv, updating it might solve this issue

virtualenv /path/to/old/virtualenv/
Christal answered 27/4, 2015 at 15:12 Comment(0)
E
3

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!

Effect answered 25/9, 2014 at 5:55 Comment(1)
had to create the lib-dynload directory and copy paste contents of Python-2.7.*/build/lib.linux-***-2.7 into itAerification
C
1

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)

Carbonado answered 17/3, 2016 at 19:20 Comment(0)
J
1
  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

Jorgejorgensen answered 29/3, 2016 at 6:23 Comment(0)
S
0

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.

Stevie answered 11/5, 2017 at 7:29 Comment(0)
L
0

I ran across this error with LetsEncrypt trying to renew and in order to fix I had to go back to the letsencrypt site and reinstall certbot because they have changed how it works on Ubuntu.

https://certbot.eff.org/lets-encrypt/ubuntuxenial-nginx

Here are the commands I ended up running, your commands may be different.

snap was not installed in my Ubuntu instance

sudo apt install snapd 
sudo snap install core; sudo snap refresh core

remove the old certbot

sudo apt-get remove certbot
sudo snap install --classic certbot

make the "certbot" command work with old config

sudo ln -s /snap/bin/certbot /usr/bin/certbot

run a couple tests

sudo certbot --nginx
sudo certbot renew --dry-run

Actually renew and restart server

sudo certbot renew
sudo /opt/bitnami/ctlscript.sh restart nginx

Update cronjob

Then I needed to update my cronjob so that it used sudo certbot renew instead of the full path to certbot that I was using before.

crontab -e

contents of crontab

42 3,15 * * * sudo certbot renew --quiet --no-self-upgrade
24 1,18 * * * sudo certbot renew --quiet --no-self-upgrade
5 4 * * 7 sudo /opt/bitnami/ctlscript.sh restart nginx
Leatherwood answered 27/12, 2020 at 18:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.