'Pyinstaller and Django Rest

I am trying to use Pyinstaller with django rest, it generates the .exe well but there is an error at the moment of executing the .exe, the error is this

ModuleNotFoundError: No module named 'rest_framework'

my question is how can I install the dependencies using Pyinstaller, or is there another way to do it.



Solution 1:[1]

This error ocurrs when you have dynamic imports in your code. In that case, pyinstaller don't include those packages in exe file. In that case you can:

  1. Add unused import of those packages in your code
  2. Tell pyinstaller to include it

One file option does not change anything in running your code. If you're create --onefile exe all files created by pyinstaller are packed to exe file, and unpacked to local temp every time when you run exe.

Other Possible Solutions are:

Solution 1: run your command from parent directory, i.e. instead of

c:\compilation\Gui>pyinstaller --name=gui manage.py

do

c:\compilation>pyinstaller --name=gui Gui\manage.py

Also Add runserver to the End of the File.

if still the Issue Persists, Then Solution 2: pyinstaller --name=gui --exclude-module=PyQt4 --exclude-module=matplotlib --clean --win-private-assemblies manage.py runserver

Solution 2:[2]

Make sure you have the required modules installed correctly. After you have installed the required modules, create hook for rest_framework and add it to ..\site-packages\PyInstaller\hooks and name the file as hook-rest_framework.py. Contents of file:

from PyInstaller.utils.hooks import collect_submodules
hiddenimports = collect_submodules('rest_framework')

This solved the same problem for me.

Solution 3:[3]

in your manage.spec file, you need add such code below:

from PyInstaller.utils.hooks import collect_submodules

hiddenimports = collect_submodules('rest_framework')

and also you need add another Dependency packages

hiddenimports.extend(
    [
        'django.contrib.contenttypes',
        'django.contrib.staticfiles',
        'django_filters',
        ....
    ]
)

and if you rest-freamwork template shows doesn't exist, so you need config the static files as blew?

datas=[(r'/env/lib/python3.6/site-packages/rest_framework/', './rest_framework'),               
(r'/env/lib/python3.6/site-packages/django_filters/', './django_filters')
]

Solution 4:[4]

In your terminal:

pip install djangorestframework
pip install markdown
pip install django-filter 

python3 can use pip3 install.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Syed Faizan
Solution 2 Pratyush Dey
Solution 3
Solution 4 ssuperczynski