Django/Python error. “ImportError: Import by filename is not supported.”

坚强是说给别人听的谎言 提交于 2019-12-01 10:06:41

问题


I wanted to install Django and have troubles with the import error. I read this topic, but the answer didn't help here. Nothing happend after I changed the env variables to the module path.

C:\Users\M>django-admin.py
Traceback (most recent call last):
  File "C:\Python27\Scripts\django-admin.py", line 5, in <module>
    management.execute_from_command_line()    
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "C:\Python27\lib\site-packages\django\core\management\__init__.py", line 302, in execute
    settings.INSTALLED_APPS
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 55, in __getattr__
    self._setup(name)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "C:\Python27\lib\importlib\__init__.py", line 37, in import_module
    __import__(name)
ImportError: Import by filename is not supported.

回答1:


DJANGO_SETTINGS_MODULE shouldn't be system path to settings file (or directory). It should be an python path to your settings file. So first, make sure that your project is in PYTHONPATH or you are inside project directory, then set correct DJANGO_SETTINGS_MODULE and after that, run your django-admin command.

You can also clean DJANGO_SETTINGS_MODULE variable if command that you're trying to issue is not related with existing django project.




回答2:


I was facing the same issue in windows. Then later I realised that the problem is with the version of Django installed. I am using python 2.7, and it seems that the import by filename is not supported by it. I believe if you use python 3 , there wont be any problem. So , I installed django 1.6.5 and the solved the problem.

pip install django==1.6.5


来源:https://stackoverflow.com/questions/34481102/django-python-error-importerror-import-by-filename-is-not-supported

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!