Python cannot find dateutil.relativedelta

Question:

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.

Asked By: RonnyKnoxville

||

Answers:

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! 😉
Answered By: mac

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.

Answered By: Jon

(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.

Answered By: Sujit Pal

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

Answered By: lofidevops