简体   繁体   English

django项目在virtualenv中不起作用

[英]django project not working in virtualenv

I recently installed a virtualenv and cloned an existing django project into it. 我最近安装了virtualenv并将一个现有的django项目克隆到其中。 When I run python manage.py runserver I'm getting the following error: 当我运行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

when I try doing application-name/application python manage.py runserver , I get the following error: 当我尝试执行application-name/application python manage.py runserver ,出现以下错误:

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

here is the application directory 这是应用程序目录

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

First, rename your application-name to application_name . 首先,将您的application-name重命名为application_name Dashes and Python source is just inviting problems, best not wake the dragons. 破折号和Python来源只是引人入胜的问题,最好不要惊醒巨龙。

Let's assume you've put it in ~/work/application_name , ie outside your virtualenv. 假设您将其放在~/work/application_name ,即在virtualenv之外。

If you didn't create the virtualenv with virtualenvwrapper and it doesn't have a setup.py file, then delete it (and install virtualenvwrapper ). 如果您没有使用virtualenvwrapper创建virtualenvwrapper并且它没有setup.py文件,则将其删除(并安装virtualenvwrapper )。

Create your virtualenv with virtualenvwrapper : 使用virtualenvwrapper创建您的virtualenvwrapper

mkvirtualenv -a ~/work/application_name -r ~/work/application_name/requirements.txt eb_virt

-a add an existing directory as a project dir (puts it on the python path for this virtualenv). -a将现有目录添加为项目目录(将其放入此virtualenv的python路径中)。 -r runs pip install -r on the argument (I'm assuming application_name/requirements.txt exists). -r在参数上运行pip install -r (我假设存在application_name/requirements.txt )。

Instead of using the -a option (and assuming application_name/setup.py exists), you can install your package in development mode : 除了使用-a选项(并假设存在application_name/setup.py ),您还可以在开发模式下安装软件包:

cd ~/work
pip install -e application_name

Either of these make ~/work/application_name part of the module search path when Python is running. 当Python运行时,这两个都将〜/ work / application_name用作模块搜索路径的一部分。 Your settings file path must start relative to a directory on Python's module search path. 您的设置文件路径必须相对于Python模块搜索路径上的目录开始。

The DJANGO_SETTINGS_MODULE environment variable, should therefore be set to 'application.settings' . 因此,应将DJANGO_SETTINGS_MODULE环境变量设置为'application.settings'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM