Structure Python application with client and server parts

与世无争的帅哥 提交于 2020-01-04 15:33:42

问题


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

myapp/
  client/
  core/
  server/
  template_files/

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.

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?


回答1:


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.

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

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.

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


来源:https://stackoverflow.com/questions/28955165/structure-python-application-with-client-and-server-parts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!