38

I am trying to run a program using paster serve, but I keep getting the error:

ImportError: No module named dateutil.relativedelta

I am running Python version 2.6.7 and dateutil version 1.5, so it should be installed.

Has anyone got any ideas as to why this would happen?

I am importing using

from dateutil.relativedelta import *

I can even see the package when I search:

/usr/lib/python2.7/site-packages/dateutil/relativedelta.pyc
/usr/lib/python2.7/site-packages/dateutil/relativedelta.py
/usr/lib/python2.7/site-packages/dateutil/relativedelta.pyo

UPDATE

Immediately I look at this and see that dateutil is only installed for Python 2.7, and I bet what I was doing was this:

sudo yum install python-dateutil

To which sudo would have switch to the default Python version (i.e., Python 2.7 instead of 2.6.4).

Solving this would have been as simple as:

su
(switch to virtual environment)
yum install python-dateutil

Using su and then switching to the virtual environment will give root access and install to the virtual Python directory. Using sudo will install libraries to the default directory, not the virtual environments site-packages.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
  • What's the import line you're using? – thegrinner Dec 07 '11 at 16:22
  • 1
    er, are you quite sure that works? `rpm` is not normally aware of virtual environments. Unless you are getting `python-dateutil` from a very unusual rpm from a non-standard fedora repository, installing a python package would copy files to the same location regardless of your virtual environment. On the other hand, becoming root (or not) and using `pip` or any other python packaging tool *should* install in the right place. In fact, most RPM's use this mechanism; but during the *build* stage, ie, by the package's maintainer. – SingleNegationElimination Oct 14 '12 at 14:43

4 Answers4

66

I also ran into this issue. The simple solution I ended up using was to add --upgrade to the end of the command. This forced it to install it even though Python thought it was installed. This resolved the issue.

So if you have this issue, try the following:

sudo pip install python-dateutil --upgrade

It can't possibly hurt anything, so there is no harm in just forcing it to be reinstalled.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jon
  • 3,212
  • 32
  • 35
  • I got this error after manually upgrading my python on Mac (OS X 10.7.5) from 2.7.1 to 2.7.3 and this solution worked for me. I am presuming that the comment from Sujit Pal hints at why the problem occurred after the upgrade -- perhaps there is a change in the library path locations for this module. – Mark Chackerian Jan 23 '13 at 17:12
  • just make sure the correct version, e.g. use 1.5 for python 2.x: sudo pip install python-dateutil==1.5 – Xiaofeng Tang Dec 10 '13 at 15:26
  • Worked for me. Not sure why, cause without the `--upgrade` flag, it said it's already installed, so not sure why it would say it's not found, when actually the version is too old. Thanks. – Eduard Luca May 14 '14 at 15:14
9

I had a similar issue but for a simpler reason. My fresh virtualenv simply didn't have dateutil installed and I didn't know the Python package name. I tried pip install dateutil, which obviously didn't work since the package name was incorrect. Running pip install python-dateutil instead worked (without resorting to sudo).

lofidevops
  • 15,528
  • 14
  • 79
  • 119
1

(The previous comment about installing python-dateutil helped me, so perhaps my comment helps someone else).

For those on Mac OS (v10.6 (Snow Leopard); I am not sure about other versions), the dateutils package is located by default at:

/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/dateutil

whereas pip install writes the package out to:

/Library/Python/2.6/site-packages

and does not update the /Library/Python/2.6/site-packages/easy-install.pth file. As a result, when you import dateutil, you will still point to the old location, you can verify this by "import dateutil; dateutil.__file__".

So what I did (probably better methods are available) was to rename the old directory (/System/Library/.../dateutil) to dateutil.obsolete and restarted Python, then ran the same set of commands again. This doesn't do anything to the path file or sys.path, but skips the old dateutils package so you can get to the new one.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Sujit Pal
  • 121
  • 1
  • 5
1

This looks like a problem of package installation to me. A troubleshooting list that comes to my mind:

  1. Verify you installed the package.
  2. If installed, verify that the files have been stored in the right directory (a directory accessible from your python interpreter (= in the PYTHONPATH, useful article here).
  3. Verify permission on those files.
  4. Restart your shell if you tried the import there.
  5. Reboot your computer (ouch... it's 10 years since I started using GNU/Linux, but I still suffer from the bad memories of Windows! ;)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mac
  • 42,153
  • 26
  • 121
  • 131