将 django 项目及其依赖项打包为独立的“产品”
我使用 Django 作为框架制作了一个小型“应用程序”。这是一个不打算部署到服务器而是在计算机上本地运行的应用程序。因此,runserver.py
工作得很好。
作为一名开发人员,我很乐意启动终端、运行 python manage.py runserver 并使用它。
但我有一些 Mac OS X 和 Windows 朋友想要使用我的应用程序,但他们没有 virtualenv
、git
或其他任何东西。
有没有办法可以将其打包为独立产品?当然,这取决于系统上安装的 Python,但是是否可以将 virtualenv
与 django
和所有内容一起打包,然后将其复制到另一个系统并制作有效吗?
甚至可能以某种守护程序模式运行运行服务器?
I've made a small little "application" utilizing Django as a framework. This is an application that is not maent to be deployed to a server but run locally on a machine. Thus the runserver.py
works great.
I, as a developer am comfortable with firing up the terminal, running python manage.py runserver
and using it.
But I have some Mac OS X and Windows friends wanting to use my application, and they don't have virtualenv
, git
or anything else.
Is there a way I can package this to be a standalone product? Of course it would depend on Python being installed on the system, but is it possible to package the virtualenv
— with django
and everything, and just copy it to another system and make it work?
And maybe even run the runserver in some kind a deamon mode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用 setuptools 和 easy_install。
这是一篇介绍性文章 。
Use setuptools and easy_install.
Here's an introductory article.
我还没有找到完美的解决方案。
我目前的方法是提供一个 docker 镜像,因为这对每个人来说都很容易使用。这包括 alpine 基础镜像,因为它很小,并且包含 python + django 和应用程序本身。您还可以包含一个像 nginx 这样的网络服务器和一个像 uwsgi 或 Gunicorn 这样的应用程序服务器,并为其公开一个端口。
因此,最终您的用户只需运行容器并访问 http://localhost:9000/ 下的 Web 应用程序或类似的东西。
这真的很方便,也是我尝试我找到的一些应用程序的首选方式。
“正确”的方法是为您所针对的每个操作系统和发行版构建一个软件包以及一个简单的 zip 捆绑包,以便人们也可以手动安装该应用程序。
要构建软件包,我建议使用 fpm。它消除了使用原生工具进行打包的大部分痛苦。然后,这些包将依赖于适当的应用程序服务器,例如 uwsgi 或 Gunicorn。
最后你可以像 apt install your-package 一样安装它,它将依赖于 python-django、uwsgi 等。
所以 位置以及将所有文件放在包中的位置每个发行版都有自己的方法。我更喜欢将所有内容放在
/usr/share/webapps/myapp/
下,并将配置放在/etc/myapp/config.py
或类似的东西下。对于 Windows 和 macOS,有诸如 PyInstaller 之类的解决方案。我还没有将它用于 django 应用程序,但它应该可以完成这项工作。您也应该在其中包含像 uwsgi 这样的应用程序服务器。
一般来说,您不想在生产环境中运行 django 开发服务器。因此,在包装时请记住这一点。
我希望这能有所帮助。
I also haven't found the perfect solution for this yet.
My current approach is to provide a docker image because that's really easy to use for everyone. This includes an alpine base image because it's tiny and python + django and the app itself. You can also include a webserver like nginx and an app server like uwsgi or gunicorn and expose a port for it.
So in the end your user would just run the container and access the web app under http://localhost:9000/ or something like this.
This is really handy and also my preferred way of trying out some app I've found.
The "proper" way would be do build a package for every OS and distribution you are targeting and a simple zip bundle so people can also install the app manually.
To build the packages I suggest using fpm. It takes most of the pain of doing the packaging with their native tools away. The packages would then depend on a proper application server like uwsgi or gunicorn.
So in the end you could then install it like
apt install your-package
and it would depend onpython-django
,uwsgi
etc.For the location and where to put all the files in the package every distribution has their own way of doing it. I prefer putting everything under
/usr/share/webapps/myapp/
and having the config under/etc/myapp/config.py
or something like that.For Windows and macOS there are solutions like PyInstaller. I haven't used it yet for a django app but it should do the job. You should include a app server like uwsgi in there too.
In generel you don't want to run the django dev server in a production environment. So keep that in mind when packaging.
I hope that helps a bit.
是的,你可以打包它。 Django 可能不是最容易做到这一点的,但其他框架的原理是相同的。您需要制作一个安装程序来安装您需要的一切。对于不同的平台,该安装程序需要不同。例如 Windows、Ubuntu、OS X 等。这也意味着每个平台的答案都有很大不同,并且只有一半的答案取决于 Django。 :-(
这有点糟糕,但这就是目前的生活。没有好的独立于平台的方式为最终用户安装软件。
Yes, you can package it. Django may not be the easiest to do this with, but the principles are the same for other frameworks. You need to make an installer that installs everything you need. And that installer needs to be different for different platforms. such as Windows, Ubuntu, OS X etc. That also means that the answer is significantly different for each platform, and only half of the answer is dependning on Django. :-(
This kinda sucks, but that's life, currently. There is no nice platform independent way to install software for end users.
有几种方法可以做到这一点。我认为您更多地寻找构建工具(包括打包)而不仅仅是 Python 解决方案。以下是我过去使用过的几个:
zc.buildout:用于构建和部署 Python 模块和应用程序,但也可以通过一些操作与其他语言一起使用。易于使用(对于构建工具)。
make:软件构建经典。几乎适用于所有语言,但对于初学者来说有点过时且难以学习。
There are several ways of doing this. I think you are looking more for build tools (which includes packaging) rather than just a Python solution. Here are a couple that I've used in the past:
zc.buildout: Used to build and deploy Python modules and applications, but is also able to work with other languages with a little massaging. Easy to use (for a build tool).
make: The software build classic. Works with practically all languages but a little archaic and hard to learn for a first timer.
新的 snap 包管理器应该非常适合这项任务。它提供了迄今为止对 Python 应用程序来说相当痛苦的所有解决方案(依赖项、解释器等),同时避免了 Docker 的复杂性。
The new snap package manager for Linux should suit the task perfectly. It provides all the solutions that were quite a pain for Python apps so far (dependencies, interpreter etc) and at the same time avoiding the complexity of Docker.
如今,Docker 可能是一个很好的答案,
用户需要首先安装 Docker,但它在 Windows 和 OSX 上运行以及Linux。
您的 Dockerfile 负责安装所有依赖项,然后运行 devserver(或者您甚至可以在容器中运行适当的 Web 服务器)
These days Docker is probably a good answer
User needs to install Docker first, but it runs on Windows and OSX as well as Linux.
Your Dockerfile takes care of installing all the dependencies and then runs the devserver (or you could even run a proper webserver in the container)