In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
How to package the Python Web project to start without installation? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
I. current situation
Python has many excellent Web development frameworks for us to use, such as Django, Flask, Sanic. Normally, we develop websites based on these Web frameworks, and eventually deploy them on Linux servers using Nginx with uWSGI or gunicorn, so that users can access them through bound domain names or IP addresses.
Yes, this is a normal situation.
Often a lot of scenes, we are not faced with a normal situation. Under normal circumstances, all environmental problems are not a problem, because operation and maintenance partners help us deal with all environmental problems, from the installation of a specific version of Python, to the installation of specific dependent libraries, to the configuration of services. There's nothing to worry about.
But what if you have to worry about all these problems? That's a really catastrophic problem. The emergence of Docker solves part of the problem, but for complete rookies, the installation and use of docker has become a problem.
Mr. State encountered this problem when he was developing MrDoc Road search documents. If you want to install MrDoc in source code, you need to:
Install Python (basically no problems)
Pip installs dependent libraries (very prone to problems)
Install dependent third-party software packages (very prone to problems)
Many friends who want to experience MrDoc directly died in the second step, at a loss as to what to do to report all kinds of mistakes.
Enthusiastic partners in the MrDoc communication group have created docker images, which solves some environmental problems, but for truly zero-based computer rookies, the command line still has its own fear attribute.
What should I do? Can a Web project written by Python be started directly like a Web service written in PHP or GO?
After wandering around on the Internet for a long time, I found that my old friend, PyInstaller, could achieve this function.
Second, packing
PyInstaller is a third-party module used to package Python programs as executable files in Python. Mr. State used to use it to package and distribute desktop graphical interface programs written by PyQt5. Let's take a look at its application in packaging Web programs.
PyInstaller officially provides a document to package the Django project, and the link is: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Executable-From-Django
According to this document, we run the following command according to the specific circumstances of the project: pyinstaller-- name=mysite mysite/manage.py
You will get a dist folder that contains all the files running the Django project. We open the command line interface and use the xxx.exe file to achieve the same effect as normal use: python manage.py. For example, run the runserver service:
Xxx.exe runserver
However, it is very likely that it will not work, and the wrong information is either a lack of modules or a lack of static files.
We need to explicitly tell PyInstaller which packages, which modules, and which files need to be packaged together.
After the last run of the pyinstaller command, a configuration file with the .spec suffix is generated under the folder, which is essentially a Python file, which we modify.
Import sys sys.setrecursionlimit (5000)
In line 3 of the file, add the above two lines of code to avoid errors at the pyinstaller indexing module level.
Then, in the datas list, add the static file directory, media file directory, and other used file directories of the Django project as tuples. This allows PyInstaller to package these files directly into the resulting folder.
Next, in the hidedenimports list, add the application module path of the Django project. You can add one by one according to each generated error report, but that would be a waste of time, so you might as well add them all directly.
After the modification, we delete the previously generated build directory and dist directory, and use the spec file to call the Pyinstaller command: pyinstaller MrDoc.spec
Wait a while, and the entire Django project will be packed, and the complete package file is as follows:
Third, effect
After packing, we open the command line interface and enter the command: mrdoc.exe runserver to launch the Django Web website, as shown in the following figure:
Visit: 127.0.0.1VOL8000 on the browser to see our MrDoc foraging document:
For ease of use, we can also write a bat script that can be started by double-clicking, eliminating the hassle of opening the command line interface to enter commands:
Start. / mrdoc.exe runserver
In this way, an installation-free, click-to-start Django website application is packaged and completed. This is the answer to the question about how to package the Python Web project without installation. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.