在 Django 中导入 python 模块时出错
在我的 Django 项目中,以下行抛出 ImportError:“没有名为 elementtree 的模块”。
from elementtree import ElementTree
但是,该模块已安装(即,我可以运行交互式 python shell,并键入该行而不会出现任何 ImportError),并且包含该模块的目录位于 PYTHONPATH 上。但是当我访问浏览器中的任何页面时,它不知何故找不到该模块,并抛出 ImportError。可能是什么原因造成的?
In my Django project, the following line throws an ImportError: "No module named elementtree".
from elementtree import ElementTree
However, the module is installed (ie, I can run an interactive python shell, and type that exact line without any ImportError), and the directory containing the module is on the PYTHONPATH. But when I access any page in a browser, it somehow can't find the module, and throws the ImportError. What could be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在 django shell 中导入
elementtree
吗:假设您有多个 python 版本并且不知道使用哪个版本来运行您的网站,请将以下内容添加到您的视图中并推送
python_ver
code> 添加到您的模板中,它将显示您正在使用的 Python 版本:您还可以在
settings.py
中以编程方式显式添加 elementtree 的路径:Can you import
elementtree
within the django shell:Assuming you have multiple python versions and do not know which one is being used to run your site, add the following to your view and push
python_ver
to your template, it will show you the Python version you are using:You can also explicitly add the path to elementtree programatically in your
settings.py
:我还遇到了跨平台问题,其中 ElementTree 可从不同系统上的不同模块获得...这最终对我有用:
可能对您有帮助,也可能没有帮助...
I've also run into cross-platform issues where ElementTree was available from different modules on different systems... this ended up working for me:
May or may not help for you...
进入您的安装目录
示例:
并检查 elementtree 和 django 是否都在其中。
如果它们都不存在,那么您可能有不同版本的 Python 的多个安装目录。
无论如何,您可以通过运行以下命令来解决您的问题:
运行两次,一次在 django 下载中,一次在 elementtree 下载中。它将把这两个下载安装到你当前的默认 python 中。
参考资料:
Go into your installation directory
Example:
And check if both elementtree and django are in there.
If they are both not there, then you probably have multiple installation directories for different versions of Python.
In any case, you can solve your problem by running this command:
Run it twice, once inside the download for django and once inside the download for elementtree. It will install both of the downloads into whatever your current default python is.
References: