Django无法导入任何包:AttributeError:__name __

时间:2019-06-25 07:41:03

标签: python django

由于某些原因,我不得不在桌面上重新安装Python。从那时起,我的Django应用程序无法正常工作。我可以创建一个,但是一旦我在应用程序中的任何位置导入了一个程序包,就无法运行python manage.py runserver

我可以运行另一个python脚本,可以启动Jupyter Notebooks(并在其中导入完全相同的软件包)。我还尝试创建安装了“ new” python的新项目,一旦添加import pandas(或django以外的任何其他软件包),我就会收到错误消息。

错误消息很长:

Watching for file changes with StatReloader
Performing system checks...

Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\core\management\commands\runserver.py", line 95, in handle
    self.run(**options)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\core\management\commands\runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 585, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 570, in start_django
    reloader.run(django_main_thread)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 288, in run
    self.run_loop()
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 294, in run_loop
    next(ticker)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 334, in tick
    for filepath, mtime in self.snapshot_files():
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 350, in snapshot_files
    for file in self.watched_files():
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 249, in watched_files
    yield from iter_all_python_module_files()
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 103, in iter_all_python_module_files
    return iter_modules_and_files(modules, frozenset(_error_files))
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\djan
go\utils\autoreload.py", line 116, in iter_modules_and_files
    if module.__name__ == '__main__':
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\py\_
apipkg.py", line 171, in __getattribute__
    return getattr(getmod(), name)
  File "C:\Users\MyName\AppData\Local\Continuum\anaconda3\lib\site-packages\py\_
error.py", line 44, in __getattr__
    raise AttributeError(name)
AttributeError: __name__

3 个答案:

答案 0 :(得分:1)

在中止IDE重构后得到了这个烦人的错误。

我建议通过以下方法重现该问题:

  • 还原git更改。
  • 确保代码中没有循环导入。
  • 重新安装虚拟环境。

不幸的是,我的错误仍然存​​在。所以我知道问题一定出在python上。确实:

  • 重新安装python 3.7解决了该问题。

答案 1 :(得分:0)

pip install django吗?

我认为当您重新安装Python时,并没有像Django一样打包安装

答案 2 :(得分:0)

卸载当前的Django软件包并安装Django 2.1.5

在Windows上使用此命令:

pip install --user django==2.1.5