简体   繁体   English

无法从自定义django包导入类

[英]Can not import class from custom django package

I am writing a custom Django module but I seem to have something wrong. 我正在编写一个自定义的Django模块,但我似乎有些不对劲。 I cannot import a class that lives in a certain file. 我无法导入属于某个文件的类。 I get the error 我收到了错误

ValueError: Unable to configure handler 'admins': Cannot resolve 'myPackage.handlers.MyHandlerClass': No module named handlers

This is the directory structure. 这是目录结构。 I believe I can import views and models with no problem. 我相信我可以毫无问题地导入视图和模型。

myPackage
├── CHANGELOG.rst
├── myPackage
│   ├── handlers .py
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── views.py
│   └── views.pyc
├── myPackage.egg-info
│   ├── dependency_links.txt
│   ├── PKG-INFO
│   ├── requires.txt
│   ├── SOURCES.txt
│   └── top_level.txt
├── MANIFEST.in
├── README.rst
├── requirements.txt
└── setup.py

There is a space in the filename of handlers .py , so python can't find a module names handlers . handlers .py的文件名中有一个空格,因此python无法找到模块名称handlers Obviously the easiest fix is to correct the filename, but for anyone actually wanting a space in the filename, import name with spaces is a syntax error, so the only way to import such a name is using __import__ . 显然,最简单的修复方法是更正文件名,但对于任何实际需要文件import name with spaces来说, import name with spaces是一种语法错误,因此导入这样一个名称的唯一方法是使用__import__ But this is really a very bad idea. 但这真是一个非常糟糕的主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM