用客户端和服务器部分构造Python应用程序 [英] Structure Python application with client and server parts

查看:80
本文介绍了用客户端和服务器部分构造Python应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个当前处于以下文件夹结构中的应用程序:

I have an application that is currently in the following folder structure:

myapp/
  client/
  core/
  server/
  template_files/

它具有服务器端和客户端组件,当我将代码部署到用户时,我也不想包括服务器端代码。 客户端服务器都需要 core 代码才能运行。

It has a server side and a client side component and when I deploy the code to a user, I don't want to include the server side code as well. Both client and server need the core code to run.

在阅读一般的Python项目结构时,我意识到我应该首先将结构更改为:

Reading about general Python project structure, I realise I should start by changing my structure to:

myapp/
  myapp/
    client/
    core/
    server/
      template_files/ (template is only needed by the server)
  bin/
  setup.py

构造目录和执行操作的最佳方法是什么

What's the best way to structure my directories and do code deployment?

推荐答案

您可能希望拥有三个单独的软件包,而不是一个,即使您的第二个结构也会生成一个软件包( python egg)包含所有子模块,这些子模块将导致服务器代码与客户端代码打包在一起。

You might want to have three separate packages instead of one, even your second structure will result in a package (python egg) that contains all the sub-modules which will result in the server code being packaged with the client code.

您想要的是将它们分为三个部分软件包(即 myapp.client myapp.core myapp.server ),它们将具有分开的目录str ,所以实际上您会得到类似的东西

What you want is to split them up into three individual packages (i.e. myapp.client, myapp.core and myapp.server) and they will have separated directory structures, so in effect you would have something like

myapp.client/
  myapp/
    client/
  setup.py
myapp.core/
  myapp/
    core/
  setup.py
myapp.server/
  myapp/
    server/
      template_files/
  setup.py

因为它们都会成为适当的python包,您可以在 setup.py 中为 myapp.client myapp.server 要求 myapp.core ,因此,如果/当您将软件包部署到pypi(或其他)上时,用户可以简单地 pip install myapp.client 将客户端库安装到其系统上,并提取所有依赖项。

As they will all become proper python packages, you can define dependencies in the setup.py for myapp.client and myapp.server to require myapp.core, so if/when you deploy the packages onto pypi (or others) your users can simply do pip install myapp.client to get the client library installed onto their system with all dependencies fetched.

您不要必须在任何地方都有 bin 。您可以利用 entry_points setup 函数中的> 属性,可让setuptools以与操作系统无关的方式为您创建二进制文件。只需在您的库中定义主要函数,然后让setuptools为您的用户创建可执行文件即可。

You don't necessarily have to have a bin in anywhere. You can take advantage of the entry_points attribute in the setup function to let setuptools create the "binary" for you in an OS agnostic manner. Just define the main function(s) inside your library and let setuptools create the executable for you for your users.

最后,您可能还想看看其他开源项目已经完成了对其库的打包,下面是一些示例:

Lastly, you might want to take a look at what other open source projects have done for packaging their libraries, here are some examples:

  • Flask
  • Jinja2
  • plone.app.discussion

这篇关于用客户端和服务器部分构造Python应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆