Django无法导入任何包:AttributeError:__name__ [英] Django fails to import any package : AttributeError: __name__

查看:108
本文介绍了Django无法导入任何包:AttributeError:__name__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

For some reasons, I had to reinstall Python on my desktop. Since then, my django applications are not working. I can create one, but as soon as I import a package anywhere in the application, I can't run python manage.py runserver

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

I can run another python script, I can start Jupyter Notebooks (and import the exact same packages in them). I also tried to create new projects with the "new" python installed, as soon as I add import pandas (or any other packages except django), I have the error.

错误消息很长:

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__

推荐答案

中止IDE重构后,请解决此烦人的错误.

Got this annoying error after aborting an IDE-refactoring.

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

I suggest the following to reproduce the problem:

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

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

Unfortunately the error was still there in my case. So I knew the problem must be with python. And indeed:

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

这篇关于Django无法导入任何包:AttributeError:__name__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆