仅限 Scrapyd - 没有模块名称 django_project.settings

Scrapyd only - No module name django_project.settings

这个错误只发生在命令 scrapyd-deploy.

Packing version r240-master
Deploying to project "foo_bot" in http://localhost:6802/addversion.json
Server response (200):

{
    "status": "error", 
    "message": "Traceback (most recent call last):
      File \"/usr/lib/python2.7/runpy.py\", line 174, in _run_module_as_main
        \"__main__\", fname, loader, pkg_name)
      File \"/usr/lib/python2.7/runpy.py\", line 72, in _run_code
        exec code in run_globals
      File \"/usr/local/lib/python2.7/dist-packages/scrapyd/runner.py\", line 40, in <module>
        main()
      File \"/usr/local/lib/python2.7/dist-packages/scrapyd/runner.py\", line 37, in main
        execute()
      File \"/usr/local/lib/python2.7/dist-packages/scrapy/cmdline.py\", line 109, in execute
        settings = get_project_settings()
      File \"/usr/local/lib/python2.7/dist-packages/scrapy/utils/project.py\", line 68, in get_project_settings
        settings.setmodule(settings_module_path, priority='project')
      File \"/usr/local/lib/python2.7/dist-packages/scrapy/settings/__init__.py\", line 292, in setmodule
        module = import_module(module)
      File \"/usr/lib/python2.7/importlib/__init__.py\", line 37, in import_module
        __import__(name)
      File \"build/bdist.linux-x86_64/egg/foo_bot/settings.py\", line 21, in <module>
      File \"/usr/local/lib/python2.7/dist-packages/django/__init__.py\", line 22, in setup
        configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
      File \"/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py\", line 56, in __getattr__
        self._setup(name)
      File \"/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py\", line 41, in _setup
        self._wrapped = Settings(settings_module)
      File \"/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py\", line 110, in __init__
        mod = importlib.import_module(self.SETTINGS_MODULE)
      File \"/usr/lib/python2.7/importlib/__init__.py\", line 37, in import_module
        __import__(name)
        ImportError: No module named foo_project.settings
    ", 
    "node_name": "ubuntu"
}

项目结构如下:

/home/dev/workspace/fufu
|--dev_venv/bin/activate
|
|--foo_bot
|  |--scrapy.cfg
|  |
|  |--foo_bot
|     |--settings.py
|     |--settings_dev.py
|     |--settings_deploy.py
|     |--items.py and blablabla
|     |--spiders
|        |--blablablaspider.py
|
|--foo_project
   |--manage.py
   |
   |--app
   |  |--models.py and blablabla
   |
   |--foo_project
      |--settings.py
      |--settings_dev.py
      |--settings_deploy.py
      |--wsgi.py and blablabla

scrapy.cfg

[settings]
default = foo_bot.settings

[deploy:local]
url = http://localhost:6801/
project = foo_bot

[deploy:development]
url = http://localhost:6802/
project = foo_bot
version = GIT

[deploy:deployment]
url = http://localhost:6800/
project = foo_bot
version = GIT

foo_bot/foo_bot/settings.py

import os
import sys
import django

########## PATH CONFIGURATION
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FALSE_BASE_DIR = os.path.dirname(BASE_DIR)
#WORKSPACE_DIR = os.path.dirname(FALSE_BASE_DIR) // fail with just scrapy crawl bar
WORKSPACE_DIR = '/'.join([os.path.expanduser('~'), 'workspace'])
BOT_NAME = 'foo_bot'
########## END PATH CONFIGURATION

########## DJANGO CONFIGURATION
DJANGO_PROJECT_PATH = '/'.join([FALSE_BASE_DIR, 'foo_project'])
DJANGO_SETTINGS_MODULE = 'foo_project.settings'
sys.path.append(DJANGO_PROJECT_PATH)
sys.path.insert(0, DJANGO_PROJECT_PATH)
os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
django.setup()
########## END DJANGO CONFIGURATION

#blablabla settings

try:
    from settings_dev import *
except ImportError:
    pass

foo_project/foo_project/settings.py

import os

########## PATH CONFIGURATION
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
FALSE_BASE_DIR = os.path.dirname(BASE_DIR)
WORKSPACE_DIR = os.path.dirname(FALSE_BASE_DIR)
PROJECT_NAME = 'foo_project'
########## END PATH CONFIGURATION

foo_project/foo_project/wsgi.py

import os

from django import conf
from django.core.wsgi import get_wsgi_application

conf.ENVIRONMENT_VARIABLE = 'DJANGO_SETTINGS_MODULE'
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'foo_project.settings')

application = get_wsgi_application()

我使用virtualenvactivate脚本末尾设置环境变量PYTHONPATHDJANGO_SETTINGS_MODULE如下:

export PYTHONPATH="/home/dev/workspace/fufu/foo_project"
export DJANGO_SETTINGS_MODULE="foo_project.settings"

我 运行 scrapy crawl barfufu/foo_bot,它 运行 成功(并通过 djangoitem 保存到数据库)。

我 运行 django-admin.pyfufu/foo_project,运行 成功,没有任何警告。

我 运行 python manage.pyfufu/foo_project,运行 成功,没有警告。

我 运行 scrapyd-deploy development -p foo_bot 与用户 dev 和 virutalenv dev_venv.

它returns错误如上。

scrapyd/home/dev/workspace/ 使用用户 dev 和端口 6802

调用

However, I have another user deploy with a project copy at /home/deploy/workspace/fufu/ with only different on the from settings_dev to from settings_deploy and those absolute paths.

I run scrapyd-deploy deployment -p foo_bot with user deploy and virutalenv deploy_venv.

It runs perfectly.

The scrapyd is called at /home/deploy/workspace/ with user deploy and port 6800.

我该如何解决这个问题?我已经看到了所有线程,我不知道为什么 dev 用户失败但 deploy 用户工作。

好的,我知道了。 这是因为 os.path.dirname() 会让我空路径。

foo_bot/foo_bot/settings.py

########## PATH CONFIGURATION
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
#FALSE_BASE_DIR = os.path.dirname(BASE_DIR)
#WORKSPACE_DIR = os.path.dirname(FALSE_BASE_DIR) // fail with just scrapy crawl bar
WORKSPACE_DIR = '/'.join([os.path.expanduser('~'), 'workspace'])
FALSE_BASE_DIR = '/'.join([WORKSPACE_DIR, 'fufu'])
BOT_NAME = 'foo_bot'
########## END PATH CONFIGURATION