当我尝试在 vercel 上部署 django 应用程序时出现错误

问题描述 投票:0回答:1

我在 django 上构建我的应用程序,所有依赖项都在文件“requirements.txt”中指定

我没有在 vercel 上创建环境变量来部署,因为我看到了一个教程,但那个人没有这样做。 我有两个文件:build_files.sh 和 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"
      }
    ]
  }

需求.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

这就是整个错误

 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

设置.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
python django pip vercel
1个回答
0
投票

使用 requirements.txt

Github Actions
上设置部署。

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

以及需求文件:

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

从您的

stderr
输出:

File "<string>", line 49, in get_config_posix
File "<string>", line 28, in find_package_name

异常:找不到有效的 pkg-config 名称。 手动指定 MYSQLCLIENT_CFLAGS 和 MYSQLCLIENT_LDFLAGS 环境变量 [输出结束]

您可以看到没有找到

pkg-config
命令,它告诉您手动设置这些环境变量。这很可能是
mysqlclient
未构建/找到的根本原因。

© www.soinside.com 2019 - 2024. All rights reserved.