2016-10-08 48 views
0

我剛開始學習Python(2.7)並面臨一個問題。我正在使用Windows 10.TypeError:相對導入需要python中的包參數

我創建了一個虛擬環境(c:\ virtualenvs \ testenv)並將其激活。我的應用程序文件夾路徑是c:\ pyprojects \ pytest。該文件夾包含所有列出的軟件包requirements.txt。

提示看起來像

(testenv) c:\pyprojects\pytest\pip install -r requirements.txt 

它成功地安裝在testenv所有需要的軟件包。然後,我跑到下面的命令

(testenv) c:\pyprojects\pytest\python manage.py runserver 

,並得到了以下error--

Unhandled exception in thread started by <function wrapper at 0x03ABF8F0> 
Traceback (most recent call last): 

    File "C:\virtualenvs\testenv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper 
    fn(*args, **kwargs) 
    File "C:\virtualenvs\testenv\lib\site-packages\django\core\management\commands\runserver.py", line 113, in inner_run 
    autoreload.raise_last_exception() 
    File "C:\virtualenvs\testenv\lib\site-packages\django\utils\autoreload.py", line 249, in raise_last_exception 
    six.reraise(*_exception) 
    File "C:\virtualenvs\testenv\lib\site-packages\django\utils\autoreload.py", line 226, in wrapper 
    fn(*args, **kwargs) 
    File "C:\virtualenvs\testenv\lib\site-packages\django\__init__.py", line 27, in setup 
    apps.populate(settings.INSTALLED_APPS) 
    File "C:\virtualenvs\testenv\lib\site-packages\django\apps\registry.py", line 85, in populate 
    app_config = AppConfig.create(entry) 
    File "C:\virtualenvs\testenv\lib\site-packages\django\apps\config.py", line 90, in create 
    module = import_module(entry) 
    File "c:\python27\Lib\importlib\__init__.py", line 30, in import_module 
    raise TypeError("relative imports require the 'package' argument") 
TypeError: relative imports require the 'package' argument 

現在我檢查的文件 - C:\ Python27 \ LIB \ importlib__init __ PY和它說

if name.startswith('.'): 
     if not package: 
      raise TypeError("relative imports require the 'package' argument") 
     level = 0 
     for character in name: 
      if character != '.': 
       break 
      level += 1 
     name = _resolve_name(name[level:], package, level) 
    __import__(name) 
    return sys.modules[name] 

我的應用程序文件夾中沒有文件,特別是以點開頭的settings.py。是不是我的APP文件夾不包含在主Python路徑中?或者我失去了一些東西。

任何幫助,高度讚賞。

+0

請顯示您的'INSTALLED_APPS'設置。 – knbk

回答

1

DJANGO_SETTINGS_MODULE有望成爲Python模塊標識符,而不是文件系統路徑。看一下django/conf/__ init__py文件,看起來你的設置模塊的相對路徑在那裏不起作用。您需要將它移動到您的sys.path中列出的目錄下,或者您應該將父目錄添加到您的sys.path並從那裏引用您的設置模塊。

相關問題