【发布时间】:2020-10-21 08:31:19
【问题描述】:
当我运行sudo systemctl status gunicorn 时,按照有关设置我的gunicorn.service 的文档和在线教程得到ModuleNotFoundError: No module named 'my-app'
我意识到某些内容导入错误,或者我的gunicorn.service 中没有列出正确的目录,但我正在努力找出与 [Service] 部分有关的问题。
我的/etc/systemd/system/gunicorn.service:
[Unit]
Description=gunicorn daemon
Requires=gunicorn.socket
After=network.target
[Service]
User=myname
Group=myname
EnvironmentFile=/home/myname/my-app/my-app/env
WorkingDirectory=/home/myname/my-app/my-app/app
ExecStart=/home/myname/env/bin/gunicorn \
--access-logfile - \
--workers 3 \
--bind unix:/run/gunicorn.sock \
my-app.app.wsgi:application
[Install]
WantedBy=multi-user.target
我的目录如下:
./
./
env/ (./ ../ bin/ include/ lib/ lib64 -> lib/ pyvenv.cfg share/)
.bash_history
.bash_logout
.bashrc
.cache/
.cloud-locale-test.skip
.gitconfig
.local/
.profile
.ssh/
.sudo_as_admin_successful
.vim/
.viminfo
my-app/
|_ .DS_Store
|_ .git/
|_ .idea/
|_ .travis.yml
|_ Dockerfile
|_ docker-compose.yml
|_ env/ (./ ../ bin/ include/ lib/ lib64 -> lib/ pyvenv.cfg share/)
|_ requirements.txt
|_ my_app/
|_ .flake8
|_ db.sqlite3
|_ env
|_ manage.py*
|_ static/
|_ app/
|_ ./
|_./
|___init__.py
|___pycache__/
|_asgi.py
|_settings.py
|_urls.py
|_wsgi.py
另外,我的settings.py,因为它可能是相关的:
from pathlib import Path
# 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.1/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'seeecret'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app'
'my-app'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'app.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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 = 'my-app.app.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Password validation
# https://docs.djangoproject.com/en/3.1/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.1/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.1/howto/static-files/
STATIC_URL = '/static/'
请你帮我处理一下我的EnvironmentFile、WorkingDirectory、ExecStart。另外,是我的WSGI_APPLICATION = 'my-app.app.wsgi.application' 还是WSGI_APPLICATION = 'app.wsgi.application'?我觉得我在许多文件中交替使用 2 时犯了这种错误,所以请让我知道我必须在哪些其他文件中对此进行更正。提前非常感谢您!
【问题讨论】:
-
我认为您必须将“myname”更改为您的帐户名称。 "WorkingDirectory=/home/
/my-app/my-app/app" -
@barbaart 我的账户名就是我的名字
-
我明白了,您在应用“app”和“my-app”之后忘记了 INSTALLED_APPS 中的 2 个逗号
-
@barbaart 很好发现,谢谢!解决了其他问题,但仍然得到相同的错误
ModuleNotFoundError: No module named 'my-app' -
你重新启动一切了吗?
标签: django nginx gunicorn wsgi systemd