[Django]-Location of static files when creating a Django exe using pyinstaller

4👍

please see https://docs.djangoproject.com/en/1.10/howto/static-files/

in your urls.py file, you should add something like blew

from django.conf import settings

from django.conf.urls.static import static

urlpatterns = [
url(r'^login$', login, name='login'),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

and in your app.spec file, add datas

datas=[('xxx/templates','xxx/templates'),
       ('xxx/static','xxx/static')],

2👍

I think I figured this one out. Like you, I was having the same issue where I could package and build my Django project with pyinstaller, but the static files could not be found when running the project from the built executable. Everything was working fine when I would run the project with manage.py, but this was a head scratcher.

The solution that worked for me was running the collectstatic function and then serving my static files from that directory instead of the app/static/app directory.

If you aren’t familiar with collectstatic it essentially searches your project directory for all of the static files in your apps and puts them in a folder on your top level directory that you specify in your settings file.

Here’s how to run it.

Here’s a link to its documentation.

Now my top-level directory looked like this.

site
    app
        admin.py
        apps.py
        models.py
        views.py
    site
        asgi.py
        settings.py
        urls.py
        wsgi.py
    media
        media files
    staticfiles
        static files I want to serve (css, js, etc)

In my settings file I specified my static file settings like this…

STATIC_URL = '/staticfiles/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'app\\static\\app')
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Then in my urls.py file I included the staticfiles directory like this…
(Note this is the urls.py file in the main site directory not in the app directory if you made one in there)

from django.contrib import admin
from django.conf import settings
from django.urls import include, path
from django.conf.urls.static import static

urlpatterns = [
    path('', include('app.urls')),
    path('admin/', admin.site.urls),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

In my spec file I was able to add the folder like this…

datas=[('..\\site\\staticfiles\\', '.\\staticfiles\\')]

And then in my html file I was able to use the static files like this…

<script src="{% static 'app/static.js' %}"></script>
<link rel="stylesheet" type="text/css" href="{% static 'app/static.css' %}">

Hopefully this helps some people if they’ve run into the same problem. Also as a reminder be sure to run collectstatic before you build especially if you’ve modified or changed any of your static files.

python manage.py collectstatic

Cheers!

0👍

First make sure that django.contrib.staticfiles is included in your INSTALLED_APPS in settings.py. Then have to insert in your settings.py for example this one:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

I hope definition of BASE_DIR you have in the top of settings.py. BASE_DIR points to a folder, where your manage.py file exists. So if you would have static files in the same dir, then you leave above setting as it is. If not, let’s say in the same dir, next to manage.py you will have folder named app, then your settings should look like:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "app/static"),
]

And you can even run:

python manage.py collectstatic

To get admin static files into your static directory.

Hope that helps.

👤turkus

0👍

This looks like it might be an issue with where the PyInstaller packed python script looks for static files. They are placed in a temporary folder and need to be accessed by an absolute path. Check out this issue: Bundling data files with PyInstaller (–onefile)

Leave a comment