【问题标题】:Gunicorn cannot find system Renviron errorGunicorn 找不到系统 Renviron 错误
【发布时间】:2018-07-16 00:57:12
【问题描述】:

我已经为此苦苦挣扎了好几天,达到了我的极限。

我正在将一个 Django 项目从使用 mod_wsgi/httpd 移植到使用 gunicorn/nginx

我已经使用 virtualenv/virtualenvwrapper 设置了一个虚拟环境,并且 pip 将需求文件和 gunicorn 安装到其中。 django 项目是一个非常复杂的站点,有很多依赖项。

当我使用

启动应用程序时
 `python manage.py runserver` 

一切运行良好,没有错误。但是,当我尝试从 gunicorn 开始时

`gunicorn wsgi` 

我收到此错误

```
    cannot find system Renviron
    Fatal error: unable to open base package
```

我什至不知道从哪里开始解决这个问题。我已将 .Renviron 文件添加到我的用户目录中,将 os.environ["Renviron"]=/path 添加到 wsgi.py 和 settings.py 文件中,将 RPy2 降级回 2.5.6。似乎没有任何帮助。我不知道还能做什么。任何帮助将不胜感激。

这是我的设置:

MacOS High Sierra (10.13.5) running in Parallels Desktop Lite 1.3.3 VM
Django==1.9.2
gunicorn=19.9.0
gevent==1.3.4
greenlet==0.4.13
whitenoise==3.3.1
rpy2==2.5.6
Homebrew==1.6.17
R=3.5.1

Django wsgi.py 文件

    """
    WSGI config for ri project.
    """
    import os, sys, socket, site
    dev_path = "/Users/evans/.virtualenvs/base_env/"

    os.environ["R_HOME"]='/usr/local/lib/R/etc/Renviron'
    os.environ["Renviron"]='/usr/local/lib/R/etc/Renviron'

    host = socket.gethostname()
    print "HOST: ",host

    site.addsitedir(dev_path+'lib/python2.7/site-packages')

    # Activate the virtual environment
    # Taken from http://thecodeship.com/deployment/deploy-django-apache-virtualenv-and-mod_wsgi/
    activate_env = os.path.expanduser(dev_path+'bin/activate_this.py')
    execfile(activate_env, dict(__file__=activate_env))

    sys.path.insert(0,'/usr/local/var/django/code')
    sys.path.insert(0,'/usr/local/var/django/code/ri')

    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ri.settings")

    from django.core.wsgi import get_wsgi_application
    application = get_wsgi_application()

Django settings.py 文件

    # Django settings for ri project.
    import socket
    import sys
    import os

    os.environ["Renviron"]="/usr/local/lib/R/etc/Renviron"
    Renviron="/usr/local/lib/R/etc/Renviron"
    R_HOME="/usr/local/lib/R/etc/Renviron"
    
    host = socket.gethostname()

    BASE_PATH="/usr/local/var/django/code"

    DEBUG = True # will not send email if True, Runserver will not serve static files if False

    if DEBUG:
        EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

    ADMINS = (('Mark Evans',),)
    MANAGERS = ADMINS
    TESTING = 'test' in sys.argv

    # explicitly naming this is asked for in django > 1.6
    TEST_RUNNER = 'django.test.runner.DiscoverRunner'

    DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', 
    # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
                       'NAME': 'foodb',                      # Or path to database file if using sqlite3.
                       # The following settings are not used with sqlite3:
                       'USER': 'fooadmin',
                       'PASSWORD': 'foo_admin',
                       'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
                       'PORT': '',                      # Set to empty string for default.
                      },
    }

    if TESTING:
        DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
                           'NAME': 'test_db',                      # Or path to database file if using sqlite3.
                           # The following settings are not used with sqlite3:
                           'USER': 'foo',
                           'PASSWORD': 'foo_admin',
                           'HOST': '',                      # Empty for localhost through domain sockets or '127.0.0.1' for localhost through TCP.
                           'PORT': '',                      # Set to empty string for default.
                          }
    }

    # Test runner with no database creation
    TEST_RUNNER = 'ri.RiTestRunner.RiTestRunner'

    # Hosts/domain names that are valid for this site; required if DEBUG is False
    # See https://docs.djangoproject.com/en/1.5/ref/settings/#allowed-hosts
    ALLOWED_HOSTS = ['localhost']

    TIME_ZONE = 'America/Los_Angeles'
    LANGUAGE_CODE = 'en-us'

    SITE_ID = 1
    USE_I18N = False
    USE_L10N = True
    USE_TZ = False

    ROOT_URLCONF = 'ri.urls'
    SETTINGS_FILE_FOLDER = BASE_PATH + '/ri'
    #default login redirect path
    LOGIN_REDIRECT_URL = '/'

    # Absolute filesystem path to the directory that will hold user-uploaded files.
    # Example: "/var/www/example.com/media/"
    MEDIA_ROOT = BASE_PATH + '/media'

    # URL that handles the media served from MEDIA_ROOT. Make sure to use a
    # trailing slash.
    # Examples: "http://example.com/media/", "http://media.example.com/"
    MEDIA_URL = '/media/'

    # Absolute path to the directory static files should be collected to.
    # Don't put anything in this directory yourself; store your static files
    # in apps' "static/" subdirectories and in STATICFILES_DIRS.
    # Example: "/var/www/example.com/static/"
    STATIC_ROOT = BASE_PATH + '/static/'
    STATIC_URL = '/static/'

    # Additional locations of static files
    STATICFILES_DIRS = ( BASE_PATH + '/static_src/',)

    # List of finder classes that know how to find static files in
    # various locations.
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder')

    ADMIN_MEDIA_PREFIX = '/static/admin/'

    # Make this unique, and don't share it with anybody.
    SECRET_KEY = 'xxxxxxxxxxxxxxxxx'

    TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [ BASE_PATH+"/ri/templates",],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                # Insert your TEMPLATE_CONTEXT_PROCESSORS here or use this
                # list if you haven't customized them:
                'django.contrib.auth.context_processors.auth',
                'django.template.context_processors.debug',
                'django.template.context_processors.i18n',
                'django.template.context_processors.media',
                'django.template.context_processors.static',
                'django.template.context_processors.tz',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
    ]

    MIDDLEWARE_CLASSES = (
        'whitenoise.middleware.WhiteNoiseMiddleware',
        'django.middleware.common.CommonMiddleware',
        'django.contrib.sessions.middleware.SessionMiddleware',
        # 'django.middleware.csrf.CsrfViewMiddleware',
        'django.contrib.auth.middleware.AuthenticationMiddleware',
        'django.contrib.messages.middleware.MessageMiddleware',
        'debug_panel.middleware.DebugPanelMiddleware',
        'debug_toolbar.middleware.DebugToolbarMiddleware',
        # Uncomment the next line for simple clickjacking protection:
        # 'django.middleware.clickjacking.XFrameOptionsMiddleware',
        # the following class logs out a user after AUTO_LOGOUT_DELAY amount of time
        'ri.middleware.AutoLogout',
    )

    # Add automatic caching and compression support
    STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

    CACHES = {
        'default': {
            'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
            'LOCATION': '127.0.0.1:11211',
        },

    # this cache backend will be used by django-debug-panel
    'debug-panel': {
        'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
        'LOCATION': '/var/tmp/debug-panel-cache',
        'OPTIONS': {
            'MAX_ENTRIES': 200
        }
    }
    }


    # Python dotted path to the WSGI application used by Django's runserver.
    WSGI_APPLICATION = 'ri.wsgi.application'

    INSTALLED_APPS = (
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.sites',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.admin',
        'django.contrib.admindocs',
        'django.contrib.postgres',
        'debug_toolbar',
        'debug_panel',

        # Put RI apps below this line in alphabetical order
        'ri',               # in order to access project, target, etc
     )

    

【问题讨论】:

    标签: python r django virtualenv gunicorn


    【解决方案1】:

    更新 - 所以我从来没有真正找到问题本身的解决方案。但是,我能够通过注释掉 settings.py 的 INSTALLED_APPS 部分中的应用程序来隔离它,直到找到负责 R 依赖项的应用程序。原来我不需要它,所以我删除了那个应用程序,现在一切正常。

    但情况仍然很奇怪。

    【讨论】:

      【解决方案2】:

      R_HOME 的值看起来不对。它应该是目录的名称,例如"/usr/local/lib/R",而不是文件。

      也就是说,R 本身可以很好地启动,但是当您尝试从另一种语言(无论是 Python、Java 或 Tcl)使用它时,似乎很容易出现这个问题(找不到系统 Renviron)。它可能需要R_HOME 环境变量,它通常不需要。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-12-07
        • 2016-09-19
        相关资源
        最近更新 更多