2017-04-21 84 views
0

我最近安裝了一個virtualenv並克隆了一個現有的django項目。當我運行python manage.py runserver,我發現了以下錯誤:django項目不能在virtualenv工作

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 
+0

假設「應用程序名稱」不是您的應用程序的名稱(因爲那是你的問題)?當前直接包含'application-name/settings.py'? – dhke

+0

這可能是問題的一部分。原來的django應用程序被命名爲'應用程序'。當我將其克隆到virtualenv中時,我將其命名爲「應用程序名稱」。所以我應該改變它的應用程序名稱/應用程序/ settings.py? – PCR

+0

我試過多次改變路徑,但一直沒有任何運氣 – PCR

回答

0

首先,將您的application-name重命名爲application_name。 Dashes和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)。 -r運行pip install -r上的論點(我假設application_name/requirements.txt存在)。

除了使用-a選項(假設application_name/setup.py存在),你可以在開發模式下安裝的軟件包:

cd ~/work 
pip install -e application_name 

這些要麼讓〜/工作/ Python的當模塊搜索路徑的應用程序名稱的一部分在跑。您的設置文件路徑必須相對於Python模塊搜索路徑上的目錄開始。

DJANGO_SETTINGS_MODULE環境變量因此應設置爲'application.settings'

+0

感謝您的詳細回覆。我如何刪除virtualenv? – PCR

+0

你只是從你的文件系統中刪除它,假設你還沒有創建任何你想保存的文件(你真的不應該自己創建任何文件)。 Virtualenvwrapper帶有一個rmvirtualenv命令(以及更多用於使用virtuakenvs的便利)。 – thebjorn