路径不一致 python django

发布于 2024-08-09 20:48:35 字数 399 浏览 3 评论 0原文

我正在尝试将 sorl-thumbnail 导入到 django 中的应用程序中。现在,我设置站点的方式是在 CentOS 5 上使用 mod_wsgi 和 cpanel,应用程序的路径在导入时必须具有项目名称...这很痛苦。

显然,这是对应用程序可移植性的担忧。我正在导入 sorl-thumbnail,在以前的应用程序中,我刚刚将 sorl.thumbnail 添加到已安装的应用程序中,并且它有效。

但是现在它会导致问题,除非我在导入路径前面有项目名称 www. 。以前从未这样做过,而且我似乎无法解决路径问题。

我还添加了 www.sorl.thumbnail ,但 sorl 文件中的其余路径有错误。关于如何解决这个问题或解决解决办法有什么想法吗?

I'm trying to import sorl-thumbnail into my app in django. Now the way that I have the site set up, using mod_wsgi on CentOS 5 with cpanel, the path for the apps must have the project name when importing... which is a pain.

Obviously this is a cause of concern with portability of the app. I'm importing sorl-thumbnail, in previous apps I've just added sorl.thumbnail to the installed apps and it's worked.

However now it's causing issues unless I have the project name www. in front of the import path. It's never done this before and I can't seem to get around the path issue.

I've added www.sorl.thumbnail also but then the rest of the paths in the sorl files have errors. Any ideas on how to remedy this or fix a work around?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

恰似旧人归 2024-08-16 20:48:35

导入时不需要使用项目名称 - 只需确保应用程序位于 python 路径上的某个位置即可。类似以下内容:

sys.path.append('/etc/django/domains/mydomain.com/myproject/')

... 在您的 .wsgi 文件中应该可以做到这一点(当然,还有您自己项目的路径)。

理想情况下,可重用应用程序无论如何都应该位于项目目录之外,因此请考虑创建一个文件夹(例如“/etc/django/lib/”)来包含可重用应用程序,并将其附加到 wsgi 处理程序中的 sys.path 中。

或者,如果您不喜欢这样,也许可以使用 virtualenv 并将可重用应用程序直接添加到站点包中。

或者,如果您不喜欢这样,请将可重用应用程序放在其他位置并将它们符号链接到站点包或 python 路径上的某个位置。

简而言之,只需确保您要导入的包/模块位于您的 python 路径中即可。如果您发现自己将项目名称或“www”添加到一堆导入路径中,那么您可能做错了什么。

You shouldn't need to use the project name when importing - just make sure that the apps are somewhere on your python path. Something along the lines of:

sys.path.append('/etc/django/domains/mydomain.com/myproject/')

... in your .wsgi file should do it (with the path to your own project, of course).

Ideally reusable apps should be outside of your project directory anyway, so consider creating a folder such as '/etc/django/lib/' to contain reusable apps and appending that to sys.path in your wsgi handler too.

Or, if you don't like that, perhaps use virtualenv and add your reusable apps directly to site-packages.

Or, if you don't like that, put your reusable apps somewhere else and symlink them to site-packages or somewhere on your python path.

In short, just make sure the package/module you're importing is on your python path. If you find yourself adding the project name or 'www' to a bunch of import paths, then you're probably doing something wrong.

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