Python 找不到 dateutil.relativedelta
我正在尝试使用 pasterserve
运行程序,但我不断收到错误:
导入错误:没有名为 dateutil.relativedelta 的模块
我正在运行 Python 版本 2.6.7 和 dateutil
版本 1.5,因此应该安装它。
有谁知道为什么会发生这种情况?
我正在导入,
from dateutil.relativedelta import *
当我搜索时,我什至可以看到包:
/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
更新
我立即查看这个,发现 dateutil
仅适用于 Python 2.7,我敢打赌我正在做的是这样的
sudo yum install python-dateutil
:其中 sudo 将切换到默认的 Python 版本(即 Python 2.7 而不是 2.6.4)。
解决这个问题很简单:
su
(switch to virtual environment)
yum install python-dateutil
使用 su
然后切换到虚拟环境将获得 root 访问权限并安装到虚拟 Python 目录。使用 sudo
会将库安装到默认目录,而不是虚拟环境站点包。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也遇到了这个问题。我最终使用的简单解决方案是将
--upgrade
添加到命令末尾。这迫使它安装它,即使 Python 认为它已经安装了。这解决了这个问题。因此,如果您遇到此问题,请尝试以下操作:
它不可能造成任何损害,因此强制重新安装它并没有什么坏处。
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:
It can't possibly hurt anything, so there is no harm in just forcing it to be reinstalled.
我有一个类似的问题,但原因更简单。我的新 virtualenv 根本没有安装 dateutil 并且我不知道 Python 包名称。我尝试了
pip install dateutil
,这显然不起作用,因为包名称不正确。运行pip install python-dateutil
反而有效(无需求助于sudo
)。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. Runningpip install python-dateutil
instead worked (without resorting tosudo
).对我来说这看起来像是软件包安装的问题。我想到的故障排除列表是:
This looks like a problem of package installation to me. A troubleshooting list that comes to my mind:
(之前关于安装 python-dateutil 的评论对我有帮助,所以也许我的评论对其他人有帮助)。
对于 Mac OS 上的用户(v10.6 (Snow Leopard);我不确定其他版本版本),dateutils 包默认位于:
而 pip install 将包写入到:
并且不更新/Library/Python/2.6/site-packages/easy-install.pth 文件。因此,当您导入 dateutil 时,您仍然会指向旧位置,您可以通过“
import dateutil; dateutil.__file__
”来验证这一点。所以我所做的(可能有更好的方法)是将旧目录(
/System/Library/.../dateutil
)重命名为dateutil.obsolete
并重新启动Python ,然后再次运行同一组命令。这不会对路径文件或 sys.path 执行任何操作,但会跳过旧的 dateutils 包,以便您可以使用新的包。(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:
whereas pip install writes the package out to:
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
) todateutil.obsolete
and restarted Python, then ran the same set of commands again. This doesn't do anything to the path file orsys.path
, but skips the old dateutils package so you can get to the new one.