Implementation of packaging django with pyinstaller

  • 2021-12-04 19:19:39
  • OfStack

Directory Step 1: Generate spec files Step 2: Building an executable using pyinstaller

Although the django project is generally released by deploying servers, there are also some cases, which may be a small data management application, which is used by several people inside. We want to package it directly into an application, which can run on ordinary machines without any python environment, and can be accessed by the intranet.
pyinstaller can be used to package python applications into executable files.

Step 1: Generate an spec file


pyi-makespec -D manage.py 

After successful execution, the following information will be displayed, indicating that you can build the executable file

now run pyinstaller.py to build the executable

A file manage. spec is generated under the directory, which is equivalent to a configuration file for building executable files. Open the file, you can look at 1, there are two main places to configure:

1. datas= [] This configuration is used to configure static files and templates files
hiddenimports= [] Copy install_apps from settings


 datas=[('/Users/huanghuan/Documents/python Learning /django/loftyha/static','./static')],
             hiddenimports=[ 'django.contrib.admin',
                    'django.contrib.auth',
                    'django.contrib.contenttypes',
                    'django.contrib.sessions',
                    'django.contrib.messages',
                    'django.contrib.staticfiles',
                    'shift',],

Step 2: Building an executable using pyinstaller


pyinstaller manage.spec 

After the above commands are executed, dist and build directories will be generated under the directories, and there is an executable file manage under the directories of dist/manage
Under the cd dist/manage directory, the command line executes the manage file


./manage runserver ip:port --noreload

--If the noreload parameter is not added, an error may be reported: RuntimeError ('Script% s does not exist.'% py_script)

Traceback (most recent call last):
File "manage.py", line 23, in < module >
File "manage.py", line 19, in main
File "django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "django/core/management/__init__.py", line 413, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "django/core/management/base.py", line 354, in run_from_argv
self.execute(*args, **cmd_options)
File "django/core/management/commands/runserver.py", line 61, in execute
super().execute(*args, **options)
File "django/core/management/base.py", line 398, in execute
output = self.handle(*args, **options)
File "django/core/management/commands/runserver.py", line 96, in handle
self.run(**options)
File "django/core/management/commands/runserver.py", line 103, in run
autoreload.run_with_reloader(self.inner_run, **options)
File "django/utils/autoreload.py", line 640, in run_with_reloader
exit_code = restart_with_reloader()
File "PyInstaller/hooks/rthooks/pyi_rth_django.py", line 72, in _restart_with_reloader
File "django/utils/autoreload.py", line 257, in restart_with_reloader
args = get_child_arguments()
File "django/utils/autoreload.py", line 244, in get_child_arguments
raise RuntimeError('Script %s does not exist.' % py_script)


Related articles: