[英]django project not working in 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
首先,將您的application-name
重命名為application_name
。 破折號和Python來源只是引人入勝的問題,最好不要驚醒巨龍。
假設您將其放在~/work/application_name
,即在virtualenv之外。
如果您沒有使用virtualenvwrapper
創建virtualenvwrapper
並且它沒有setup.py
文件,則將其刪除(並安裝virtualenvwrapper
)。
使用virtualenvwrapper
創建您的virtualenvwrapper
:
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'
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.