Python django项目在virtualenv中不工作

Python django项目在virtualenv中不工作,python,django,virtualenv,Python,Django,Virtualenv,我最近安装了一个virtualenv,并将一个现有的django项目克隆到其中。运行python manage.py runserver时,出现以下错误: Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "/Users/user/eb-virt/lib/python2.7/site-pac

我最近安装了一个virtualenv,并将一个现有的django项目克隆到其中。运行
python manage.py runserver
时,出现以下错误:

Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 8, in <module>
    from django.core.servers.basehttp import AdminMediaHandler, run, WSGIServerException, get_internal_wsgi_application
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/core/servers/basehttp.py", line 26, in <module>
    from django.views import static
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/views/static.py", line 95, in <module>
    template_translatable = ugettext_noop(u"Index of %(directory)s")
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 75, in gettext_noop
    return _trans.gettext_noop(message)
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/utils/translation/__init__.py", line 48, in __getattr__
    if settings.USE_I18N:
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/user/eb-virt/lib/python2.7/site-packages/django/conf/__init__.py", line 95, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'application-name.settings' (Is it on sys.path?): No module named application-name.settings

首先,将
应用程序名称
重命名为
应用程序名称
。破折号和Python源代码只是在招惹问题,最好不要惊动巨龙

让我们假设您已将其放入
~/work/application\u name
,即在您的VirtualNV之外

如果您没有使用
virtualenvwrapper
创建virtualenv,并且它没有
setup.py
文件,请删除它(并安装
virtualenvwrapper

使用
virtualenvwrapper
创建您的virtualenv:

mkvirtualenv -a ~/work/application_name -r ~/work/application_name/requirements.txt eb_virt
-a
将现有目录添加为项目目录(将其置于此virtualenv的python路径上)
-r
在参数上运行
pip install-r
(我假设
application\u name/requirements.txt
存在)

不使用
-a
选项(并假设
应用程序\u name/setup.py
存在),您可以在开发模式下安装软件包:

cd ~/work
pip install -e application_name
当Python运行时,这两个选项中的任何一个都使~/work/application_name成为模块搜索路径的一部分。设置文件路径必须相对于Python模块搜索路径上的目录开始


因此,
DJANGO\u SETTINGS\u模块
环境变量应设置为
'application.SETTINGS'

,假设
应用程序名称
不是应用程序的名称(因为这是您的问题)?当前文件直接包含可能是问题的一部分的
应用程序名称/settings.py
。最初的django应用程序名为“application”。当我将它克隆到virtualenv中时,我将它命名为“应用程序名”。那么,我应该更改它application name/application/settings.py吗?我已经尝试了多次更改路径,但是对于
settings.py
的位置没有任何运气。当然,
应用程序名
不是有效的python标识符。虽然
importlib
似乎可以处理这样的名称,但我不太确定生态系统的其余部分。@PCR,在运行
python manage.py runserver
之前是否激活了virtualenv?感谢您的详细回复。如何删除virtualenv?您只需从文件系统中删除它,假设您尚未在其中创建任何要保存的文件(您真的不应该自己在那里创建任何文件)。Virtualenvwrapper附带rmvirtualenv命令(以及使用virtualenv的更多便利)。
cd ~/work
pip install -e application_name