Search code examples
pythondjangopipvercel

Error attempting to deploy my Django app on Vercel


I build my app on Django, with all my dependencies specified in the "requirements.txt" file.

I didn't create an environment variable on Vercel to deploy because I saw a tutorial where it wasn't needed.

I have two files: build_files.sh and vercel.json

build_files.sh:

pip install -r requirements.txt
python3 manage.py collectstatic --noinput

vercel.json:

{
    "version": 2,
    "builds": [
      {
        "src": "gestor_backend/wsgi.py",
        "use": "@vercel/python",
        "config": {
          "maxLambdaSize": "15mb",
          "runtime": "python3.8"
        }
      },
      {
        "src": "build_files.sh",
        "use": "@vercel/static-build",
        "config": {
          "distDir": "staticfiles"
        }
      }
    ],
    "routes": [
      {
        "src": "/static/(.*)",
        "dest": "/static/$1"
      },
      {
        "src": "/(.*)",
        "dest": "gestor_backend/wsgi.py"
      }
    ]
  }

requirements.txt:

asgiref==3.7.2
backports.zoneinfo==0.2.1
certifi==2024.2.2
cffi==1.16.0
charset-normalizer==3.3.2
cryptography==42.0.5
defusedxml==0.8.0rc2
Django==4.2
django-cors-headers==4.3.1
django-templated-mail==1.1.1
djangorestframework==3.14.0
djangorestframework-simplejwt==5.3.1
djoser==2.2.2
idna==3.6
mysqlclient==2.2.4
oauthlib==3.2.2
pycparser==2.21
PyJWT==2.8.0
python3-openid==3.2.0
pytz==2023.3.post1
requests==2.31.0
requests-oauthlib==1.3.1
social-auth-app-django==5.4.0
social-auth-core==4.5.3
sqlparse==0.4.4
typing_extensions==4.9.0
tzdata==2023.4
urllib3==2.2.1

This is the entire error message:

 error: subprocess-exited-with-error
  
  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [24 lines of output]
      Trying pkg-config --exists mysqlclient
      Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1.
      Trying pkg-config --exists mariadb
      Command 'pkg-config --exists mariadb' returned non-zero exit status 1.
      Trying pkg-config --exists libmariadb
      Command 'pkg-config --exists libmariadb' returned non-zero exit status 1.
      Traceback (most recent call last):
        File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
          json_out['return_val'] = hook(**hook_input['kwargs'])
        File "/usr/local/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
          return hook(config_settings)
        File "/tmp/pip-build-env-9xblfbki/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
          return self._get_build_requires(config_settings, requirements=['wheel'])
        File "/tmp/pip-build-env-9xblfbki/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
          self.run_setup()
        File "/tmp/pip-build-env-9xblfbki/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 311, in run_setup
          exec(code, locals())
        File "<string>", line 155, in <module>
        File "<string>", line 49, in get_config_posix
        File "<string>", line 28, in find_package_name
      Exception: Can not find valid pkg-config name.
      Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
      [end of output]
  
  note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
note: This error originates from a subprocess, and is likely not a problem with pip.
[notice] A new release of pip is available: 23.3.2 -> 24.0
[notice] To update, run: pip install --upgrade pip
Traceback (most recent call last):
  File "/vercel/path0/manage.py", line 11, in main
    from django.core.management import execute_from_command_line
ModuleNotFoundError: No module named 'django'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
  File "/vercel/path0/manage.py", line 22, in <module>
    main()
  File "/vercel/path0/manage.py", line 13, in main
    raise ImportError(
ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
Error: Command "./build_files.sh" exited with 1

settings.py:

"""
Django settings for gestion_api project.

Generated by 'django-admin startproject' using Django 3.2.5.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path
import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-i1ws!an4=g)=w+t3uykos*cw15=za%lsyuz0si7h5@o5i=8opb'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['.vercel.app']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'back',
    'corsheaders',
    'rest_framework',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

CORS_ALLOWED_ORIGINS = [
    "https://gestor-de-gastos.pages.dev",
]

ROOT_URLCONF = 'gestion_api.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'build')],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'gestion_api.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'blhxbsdfdpwwotaucuuz',
        'USER': 'uwsbtwwudwfinla4',
        'PASSWORD': '//password',
        'HOST': 'blhxbsdfdpwwotaucuuz-mysql.services.clever-cloud.com',
        'PORT': '3306'
    }
}

# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

wsgi.py:

WSGI config for gestion_api project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'gestion_api.settings')

application = get_wsgi_application()

app = application

Solution

  • Setting up the deployment on Github Actions using the requirements.txt.

    name: SportsWarehouse 
    
    run-name: ${{ github.actor }} pushed
    
    on: [push]
    
    jobs:
      run-nba-database-test:
        runs-on: ubuntu-latest
    
        services:
          mysql:
            image: mysql:latest
            env:
              MYSQL_ROOT_PASSWORD: P@55W0RD
              MYSQL_DATABASE: test_db #nba_stats
              MYSQLCLIENT_LDFLAGS: $(pkg-config --libs mysqlclient)
              MYSQLCLIENT_CFLAGS: $(pkg-config --cflags mysqlclient)
            ports:
              - 3306:3306
            options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
    
        steps:
          - uses: actions/checkout@v4
            name: Checkout
    
          - name: Set up Python-3.9
            uses: actions/setup-python@v2
            with:
              python-version: '3.9'
              architecture: 'x64'
    
          - name: Cache Pip Install
            uses: actions/cache@v2
            with:
              path: ~/.cache/pip
              key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }}
              restore-keys:
                ${{ runner.os }}-pip-
    
          - name: Pip Install
            run: |
              python3 -m pip install mysqlclient && python3 -m pip install -r requirements.txt
    
          - name: NBA Database Test
            run: |
              cd Test/
              pytest -v -s test.py || echo
    

    And the requirements file:

    mysql-connector-python
    bs4
    pymysql
    pytest
    cryptography
    ##
    asgiref==3.7.2
    backports.zoneinfo==0.2.1
    certifi==2024.2.2
    cffi==1.16.0
    charset-normalizer==3.3.2
    cryptography==42.0.5
    defusedxml==0.8.0rc2
    Django==4.2
    django-cors-headers==4.3.1
    django-templated-mail==1.1.1
    djangorestframework==3.14.0
    djangorestframework-simplejwt==5.3.1
    djoser==2.2.2
    idna==3.6
    mysqlclient==2.2.4
    oauthlib==3.2.2
    pycparser==2.21
    PyJWT==2.8.0
    python3-openid==3.2.0
    pytz==2023.3.post1
    requests==2.31.0
    requests-oauthlib==1.3.1
    social-auth-app-django==5.4.0
    social-auth-core==4.5.3
    sqlparse==0.4.4
    typing_extensions==4.9.0
    tzdata==2023.4
    urllib3==2.2.1
    

    From your stderr output:

    File "<string>", line 49, in get_config_posix
    File "<string>", line 28, in find_package_name
    Exception: Can not find valid pkg-config name.
    Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
    [end of output]
    

    You can see that the pkg-config name is not found and it is telling you to set those environment variables manually. This is most likely the root cause of the mysqlclient not being built/found.

    In the main.yml of the Github Actions file you can see that I am exporting those as environment variables.