如何在开发环境中模拟 Django 项目的路径,使其与生产环境相同?

发布于 2024-12-15 10:44:14 字数 594 浏览 2 评论 0原文

我需要设置 Django 开发环境。

我做了一个 git clone 并从本地计算机上的生产环境中提取了所有 django 项目文件(a href="http://vagrantup.com/" rel="nofollow">Vagrant 启用虚拟机)。

问题是我的本地计算机的项目路径与我的生产路径不同(并且我无法更改它),因此在本地计算机上查找 INSTALLED_APPS 下所述的模块时遇到问题。

例如,在生产中,我的项目位于 /myproject 文件夹中,而在我的本地计算机上,位于 /vagrant/web/myproject 下。

在生产中,我像这样访问我的应用程序模块:

INSTALLED_APPS = ( 'myproject.myapp')

也在 Django 应用程序中,我像这样访问各种应用程序模块:

from myproject.myapp.models import *

我需要做什么来模拟我的开发盒上模块的生产路径,这样我就不会必须更改本地计算机上模块的路径吗?

I need to setup a Django dev environment.

I did a git clone and pulled all the django project files from production on my local machine (a Vagrant enabled VM).

The problem is that my local machine has a different path to the project than my production ( and I can't change that) so it's having problems finding modules stated under INSTALLED_APPS on my local machine.

For example on the production my project is on the /myproject folder while on my local machine is under /vagrant/web/myproject.

On the production I'm accessing my app modules like this:

INSTALLED_APPS = ( 'myproject.myapp')

Also within the Django apps I'm accessing various app modules like this:

from myproject.myapp.models import *

What do I need to do to emulate production paths to my modules on my dev box so I don't have to change the paths to the modules on my local machine?

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

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

发布评论

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

评论(3

可爱暴击 2024-12-22 10:44:15

只要 settings.py 中没有硬编码路径,应用程序目录的路径就不重要,您使用的是什么 Web 服务器?

The path to your application directory should not matter there as long as you dont have hard-coded paths in settings.py, what web server are you using?

有木有妳兜一样 2024-12-22 10:44:15

在你的settings.py中:

import os
prj_root = os.path.realpath(os.path.dirname(__file__))

prj_root 将是你的根项目文件夹的路径

In your settings.py:

import os
prj_root = os.path.realpath(os.path.dirname(__file__))

And prj_root will be path to your root project folder

瀞厅☆埖开 2024-12-22 10:44:14

如果您正在进行项目相对导入,您所需要做的就是确保项目正上方的路径位于 PYTHONPATH 上。

您只需在命令行中发出以下命令:

export PYTHONPATH='/vagrant/web'

如果您使用的是 virtualenv,则可以将该行添加到环境的 bin/activate 文件中。

If you're doing project-relative imports, all you need to do is ensure that the path directly above your project is on the PYTHONPATH.

You need only issue the following at the command line:

export PYTHONPATH='/vagrant/web'

If you're using virtualenv, you can add that line to your environment's bin/activate file.

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