Python 找不到 dateutil.relativedelta

发布于 2024-12-20 01:31:26 字数 995 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

彼岸花ソ最美的依靠 2024-12-27 01:31:26

我也遇到了这个问题。我最终使用的简单解决方案是将 --upgrade 添加到命令末尾。这迫使它安装它,即使 Python 认为它已经安装了。这解决了这个问题。

因此,如果您遇到此问题,请尝试以下操作:

sudo pip install python-dateutil --upgrade

它不可能造成任何损害,因此强制重新安装它并没有什么坏处。

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.

第七度阳光i 2024-12-27 01:31:26

我有一个类似的问题,但原因更简单。我的新 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. Running pip install python-dateutil instead worked (without resorting to sudo).

浊酒尽余欢 2024-12-27 01:31:26

对我来说这看起来像是软件包安装的问题。我想到的故障排除列表是:

  1. 验证您是否安装了该软件包。
  2. 如果已安装,请验证文件是否已存储在正确的目录中(可从 python 解释器访问的目录(= PYTHONPATH 中,有用的文章 这里)。
  3. 验证这些文件的权限。
  4. 如果您尝试在那里导入,请重新启动您的 shell。
  5. 重新启动您的计算机(哎呀...自从我开始使用以来已经 10 年了GNU/Linux,但我仍然对 Windows 怀有不好的回忆!

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! ;)
笑脸一如从前 2024-12-27 01:31:26

(之前关于安装 python-dateutil 的评论对我有帮助,所以也许我的评论对其他人有帮助)。

对于 Mac OS 上的用户(v10.6 (Snow Leopard);我不确定其他版本版本),dateutils 包默认位于:

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

而 pip install 将包写入到:

/Library/Python/2.6/site-packages

并且不更新/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:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文