Python cannot find dateutil.relativedelta
Asked Answered
S

4

40

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.

Smelser answered 7/12, 2011 at 16:19 Comment(2)
What's the import line you're using?Mick
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.Mccall
L
69

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.

Lao answered 14/10, 2012 at 14:33 Comment(3)
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.Scintillant
just make sure the correct version, e.g. use 1.5 for python 2.x: sudo pip install python-dateutil==1.5Dahabeah
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.Vassell
G
10

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

Guggenheim answered 4/9, 2013 at 8:23 Comment(0)
I
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! ;)
Involuntary answered 7/12, 2011 at 16:25 Comment(0)
S
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.

Septillion answered 19/1, 2013 at 18:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.