django项目不在virtualenv工作

时间:2017-04-21 18:28:21

标签: 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-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

当我尝试application-name/application python manage.py runserver时,我收到以下错误:

python: can't open file 'manage.py': [Errno 2] No such file or directory

这是应用程序目录

application-name/
    manage.py
    application/
       __init__.py
       urls.py
       wsgi.py
       settings.py

1 个答案:

答案 0 :(得分:0)

首先,将您的application-name重命名为application_name。破折号和Python源只是引发问题,最好不要叫醒龙。

让我们假设你已经把它放在~/work/application_name中,即在你的virtualenv之外。

如果您没有使用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_name/requirements.txt存在)。

您可以在开发模式下安装软件包,而不是使用-a选项(假设存在application_name/setup.py):

cd ~/work
pip install -e application_name

当Python运行时,这些中的任何一个都使〜/ work / application_name成为模块搜索路径的一部分。您的设置文件路径必须相对于Python模块搜索路径上的目录开始。

DJANGO_SETTINGS_MODULE环境变量因此应设置为'application.settings'