如何在Django中重用应用程序

时间:2021-09-07 18:14:25

I am trying to create my first site in Django and as I'm looking for example apps out there to draw inspiration from, I constantly stumble upon a term called "reusable apps".

我正在尝试在Django上创建我的第一个站点,当我寻找示例应用程序以从中获得灵感时,我不断地偶然发现一个术语,叫做“可重用应用程序”。

I understand the concept of an app that is reusable easy enough, but the means of reusing an app in Django are quite lost for me. Few questions that are bugging me in the whole business are:

我理解应用程序的概念,它的可重用性非常简单,但是在Django中重用应用程序的方法对我来说是很不容易的。在整个行业中,很少有问题困扰着我:

What is the preferred way to re-use an existing Django app? Where do I put it and how do I reference it?

重用现有Django应用程序的首选方式是什么?我应该把它放在哪里,我该如何引用它?

From what I understand, the recommendation is to put it on your "PYTHONPATH", but that breaks as soon as I need to deploy my app to a remote location that I have limited access to (e.g. on a hosting service).

据我所知,我的建议是把它放到你的“PYTHONPATH”上,但是当我需要将我的应用部署到一个我无法访问的远程位置(例如在托管服务上)时,这个建议就失效了。

So, if I develop my site on my local computer and intend to deploy it on an ISP where I only have ftp access, how do I re-use 3rd party Django apps so that if I deploy my site, the site keeps working (e.g. the only thing I can count on is that the service provider has Python 2.5 and Django 1.x installed)?

如果我发展我的网站在我的本地计算机,打算将其部署在ISP我只有ftp访问,我怎么重用第三方Django应用程序,这样如果我部署的网站,该网站一直工作(例如我唯一可以依靠的是服务提供者Python 2.5和Django 1。x安装)?

How do I organize my Django project so that I could easily deploy it along with all of the reusable apps I want to use?

我如何组织我的Django项目,这样我就可以轻松地将它与我想要使用的所有可重用应用程序一起部署?

2 个解决方案

#1


26  

In general, the only thing required to use a reusable app is to make sure it's on sys.path, so that you can import it from Python code. In most cases (if the author follows best practice), the reusable app tarball or bundle will contain a top-level directory with docs, a README, a setup.py, and then a subdirectory containing the actual app (see django-voting for an example; the app itself is in the "voting" subdirectory). This subdirectory is what needs to be placed in your Python path. Possible methods for doing that include:

通常,使用可重用应用程序所需要的唯一事情是确保它在sys上。路径,以便您可以从Python代码中导入它。在大多数情况下(如果作者遵循最佳实践),可重用的应用程序tarball或bundle将包含带有文档、自述和设置的*目录。然后是包含实际应用程序的子目录(参见django-voting for an示例;应用程序本身位于“投票”子目录中)。这个子目录是需要放在Python路径中的。可能的方法包括:

  • running pip install appname, if the app has been uploaded to PyPI (these days most are)
  • 运行pip安装appname,如果应用程序已经上传至PyPI(现在大多数都是)
  • installing the app with setup.py install (this has the same result as pip install appname, but requires that you first download and unpack the code yourself; pip will do that for you)
  • 安装应用程序与设置。py安装(这与pip安装appname具有相同的结果,但是需要您自己先下载并解压代码;pip会帮你)
  • manually symlinking the code directory to your Python site-packages directory
  • 手动将代码目录符号链接到Python站点包目录
  • using software like virtualenv to create a "virtual Python environment" that has its own site-packages directory, and then running setup.py install or pip install appname with that virtualenv active, or placing or symlinking the app in the virtualenv's site-packages (highly recommended over all the "global installation" options, if you value your future sanity)
  • 使用类似virtualenv的软件创建一个“虚拟Python环境”,该环境具有自己的站点包目录,然后运行setup。py安装或pip安装appname时使用该virtualenv活动,或者在virtualenv的站点包中放置或符号链接应用程序(在所有“全局安装”选项中强烈推荐,如果您重视您未来的完整性)
  • placing the application in some directory where you intend to place various apps, and then adding that directory to the PYTHONPATH environment variable
  • 将应用程序放在要放置各种应用程序的某个目录中,然后将该目录添加到PYTHONPATH环境变量中

You'll know you've got it in the right place if you can fire up a Python interpreter and "import voting" (for example) without getting an ImportError.

如果您能够启动一个Python解释器并“导入投票”(例如)而不产生重大的恐惧,那么您将知道您已经在正确的位置获得了它。

On a server where you have FTP access only, your only option is really the last one, and they have to set it up for you. If they claim to support Django they must provide some place where you can upload packages and they will be available for importing in Python. Without knowing details of your webhost, it's impossible to say how they structure that for you.

在只有FTP访问权限的服务器上,您的唯一选项实际上是最后一个选项,他们必须为您设置它。如果他们声称支持Django,他们必须提供一个可以上传包的地方,并且可以使用Python导入包。如果你不知道你的网络主机的细节,你就不可能知道它们是如何为你构建的。

#2


2  

An old question, but here's what I do:

这是一个老问题,但我要做的是:

If you're using a version control system (VCS), I suggest putting all of the reusable apps and libraries (including django) that your software needs in the VCS. If you don't want to put them directly under your project root, you can modify settings.py to add their location to sys.path.

如果您使用的是版本控制系统(VCS),我建议将所有可重用的应用程序和库(包括django)放到VCS中。如果不想将它们直接放在项目根目录下,可以修改设置。将其位置添加到sys.path。

After that deployment is as simple as cloning or checking out the VCS repository to wherever you want to use it.

在此部署之后,可以简单地将VCS存储库复制到您想使用它的任何地方。

This has two added benefits:

这有两个额外的好处:

  • Version mismatches; your software always uses the version that you tested it with, and not the version that was available at the time of deployment.
  • 版本不匹配;您的软件总是使用您测试过的版本,而不是部署时可用的版本。
  • If multiple people work on the project, nobody else has to deal with installing the dependencies.
  • 如果有多人在项目上工作,没有人需要处理依赖项的安装。

When it's time to update a component's version, update it in your VCS and then propagate the update to your deployments via it.

当需要更新组件的版本时,在VCS将其更新,然后通过它将更新传播到部署中。

#1


26  

In general, the only thing required to use a reusable app is to make sure it's on sys.path, so that you can import it from Python code. In most cases (if the author follows best practice), the reusable app tarball or bundle will contain a top-level directory with docs, a README, a setup.py, and then a subdirectory containing the actual app (see django-voting for an example; the app itself is in the "voting" subdirectory). This subdirectory is what needs to be placed in your Python path. Possible methods for doing that include:

通常,使用可重用应用程序所需要的唯一事情是确保它在sys上。路径,以便您可以从Python代码中导入它。在大多数情况下(如果作者遵循最佳实践),可重用的应用程序tarball或bundle将包含带有文档、自述和设置的*目录。然后是包含实际应用程序的子目录(参见django-voting for an示例;应用程序本身位于“投票”子目录中)。这个子目录是需要放在Python路径中的。可能的方法包括:

  • running pip install appname, if the app has been uploaded to PyPI (these days most are)
  • 运行pip安装appname,如果应用程序已经上传至PyPI(现在大多数都是)
  • installing the app with setup.py install (this has the same result as pip install appname, but requires that you first download and unpack the code yourself; pip will do that for you)
  • 安装应用程序与设置。py安装(这与pip安装appname具有相同的结果,但是需要您自己先下载并解压代码;pip会帮你)
  • manually symlinking the code directory to your Python site-packages directory
  • 手动将代码目录符号链接到Python站点包目录
  • using software like virtualenv to create a "virtual Python environment" that has its own site-packages directory, and then running setup.py install or pip install appname with that virtualenv active, or placing or symlinking the app in the virtualenv's site-packages (highly recommended over all the "global installation" options, if you value your future sanity)
  • 使用类似virtualenv的软件创建一个“虚拟Python环境”,该环境具有自己的站点包目录,然后运行setup。py安装或pip安装appname时使用该virtualenv活动,或者在virtualenv的站点包中放置或符号链接应用程序(在所有“全局安装”选项中强烈推荐,如果您重视您未来的完整性)
  • placing the application in some directory where you intend to place various apps, and then adding that directory to the PYTHONPATH environment variable
  • 将应用程序放在要放置各种应用程序的某个目录中,然后将该目录添加到PYTHONPATH环境变量中

You'll know you've got it in the right place if you can fire up a Python interpreter and "import voting" (for example) without getting an ImportError.

如果您能够启动一个Python解释器并“导入投票”(例如)而不产生重大的恐惧,那么您将知道您已经在正确的位置获得了它。

On a server where you have FTP access only, your only option is really the last one, and they have to set it up for you. If they claim to support Django they must provide some place where you can upload packages and they will be available for importing in Python. Without knowing details of your webhost, it's impossible to say how they structure that for you.

在只有FTP访问权限的服务器上,您的唯一选项实际上是最后一个选项,他们必须为您设置它。如果他们声称支持Django,他们必须提供一个可以上传包的地方,并且可以使用Python导入包。如果你不知道你的网络主机的细节,你就不可能知道它们是如何为你构建的。

#2


2  

An old question, but here's what I do:

这是一个老问题,但我要做的是:

If you're using a version control system (VCS), I suggest putting all of the reusable apps and libraries (including django) that your software needs in the VCS. If you don't want to put them directly under your project root, you can modify settings.py to add their location to sys.path.

如果您使用的是版本控制系统(VCS),我建议将所有可重用的应用程序和库(包括django)放到VCS中。如果不想将它们直接放在项目根目录下,可以修改设置。将其位置添加到sys.path。

After that deployment is as simple as cloning or checking out the VCS repository to wherever you want to use it.

在此部署之后,可以简单地将VCS存储库复制到您想使用它的任何地方。

This has two added benefits:

这有两个额外的好处:

  • Version mismatches; your software always uses the version that you tested it with, and not the version that was available at the time of deployment.
  • 版本不匹配;您的软件总是使用您测试过的版本,而不是部署时可用的版本。
  • If multiple people work on the project, nobody else has to deal with installing the dependencies.
  • 如果有多人在项目上工作,没有人需要处理依赖项的安装。

When it's time to update a component's version, update it in your VCS and then propagate the update to your deployments via it.

当需要更新组件的版本时,在VCS将其更新,然后通过它将更新传播到部署中。