带有 CherryPy 的 Django - ModuleNotFound 错误
Django with CherryPy - ModuleNotFound errors
我发现 a gist 可以让我将 CherryPy 网络服务器集成到我的程序中,并通过该 CherryPy 网络服务器 运行 一个 Django 项目。但是我在使它正常工作时遇到了一些困难。
这是我代码中的 Class 要点,基本没有变化
class ClientThread(object):
HOST = "127.0.0.1"
PORT = int("3852")
DIR = "/home/user/project/"
STATIC_ROOT = DIR + '/clientFiles/static/'
def mount_static(self, url, root):
"""
:param url: Relative url
:param root: Path to static files root
"""
config = {
'tools.staticdir.on': True,
'tools.staticdir.dir': root,
'tools.expires.on': True,
'tools.expires.secs': 86400
}
cherrypy.tree.mount(None, url, {'/': config})
def run(self):
cherrypy.config.update({
'server.socket_host': self.HOST,
'server.socket_port': self.PORT,
'engine.autoreload_on': False,
'log.screen': True
})
self.mount_static(settings.STATIC_URL, self.STATIC_ROOT)
cherrypy.tree.graft(WSGIHandler())
cherrypy.engine.start()
print("[Info]: Client started. Use http://{0}:{1} to access the WebApp.".format(self.HOST,self.PORT))
cherrypy.engine.block()
def init(self):
print("[Info]: Starting Client WebApp...")
cThread = Thread(target=self.run)
cThread.start()
整个class是通过ClientThread().init()
发起的
我的应用程序顶部的要点的前几行(按顺序):
import os
from threading import Timer
os.environ["DJANGO_SETTINGS_MODULE"] = "clientFiles.settings"
import django
django.setup()
import cherrypy
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
我的项目布局如下:
.
├── clientFiles
│ ├── clientFiles
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ └── settings.cpython-37.pyc
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── __init__.py
│ ├── manage.py
│ ├── __pycache__
│ │ └── __init__.cpython-37.pyc
│ └── static
├── main.py
我在 django-admin startproject clientFiles
.
的根目录中启动了我的 Django 项目
当我尝试 运行 我的程序时,我得到:
Traceback (most recent call last):
File "main.py", line 26, in <module>
django.setup()
File "/usr/local/lib/python3.7/dist-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/local/lib/python3.7/dist-packages/django/conf/__init__.py", line 83, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.7/dist-packages/django/conf/__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.7/dist-packages/django/conf/__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'clientFiles.settings'
看起来像是模块加载问题,对吧?
所以我尝试将 os.environ["DJANGO_SETTINGS_MODULE"] = "clientFiles.settings"
更改为 os.environ["DJANGO_SETTINGS_MODULE"] = "clientFiles.clientFiles.settings"
因为我的 Django url 在 clientFiles/clientFiles
这样做之后,我的应用程序启动表明 CherryPy 启动没有问题:
[24/Dec/2020:04:37:14] ENGINE Bus STARTING
[24/Dec/2020:04:37:14] ENGINE Started monitor thread 'Autoreloader'.
[24/Dec/2020:04:37:14] ENGINE Serving on http://127.0.0.1:3852
[24/Dec/2020:04:37:14] ENGINE Bus STARTED
[Info]: Client started. Use http://127.0.0.1:3852 to access the WebApp.
但是当我尝试访问位于 http://127.0.0.1:3852 的站点时,我得到:
ModuleNotFoundError at /
No module named 'clientFiles.urls'
Request Method: GET
Request URL: http://127.0.0.1:3852/
Django Version: 3.1.4
Exception Type: ModuleNotFoundError
Exception Value:
No module named 'clientFiles.urls'
Exception Location: <frozen importlib._bootstrap>, line 965, in _find_and_load_unlocked
Python Executable: /usr/bin/python3
Python Version: 3.7.3
Python Path:
['/home/user/project',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/user/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Thu, 24 Dec 2020 04:38:14 +0000
在控制台中我得到:
> Internal Server Error: /
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/base.py", line 165, in _get_response
callback, callback_args, callback_kwargs = self.resolve_request(request)
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/base.py", line 288, in resolve_request
resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python3.7/dist-packages/django/urls/resolvers.py", line 545, in resolve
for pattern in self.url_patterns:
File "/usr/local/lib/python3.7/dist-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/dist-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.7/dist-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/dist-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'clientFiles.urls'
好的,所以目录结构可能有问题....
我将 clientFiles/clientFiles 的内容移动到 clientFiles 中,所以我有这个:
.
├── clientFiles
│ ├── asgi.py
│ ├── __init__.py
│ ├── manage.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ └── settings.cpython-37.pyc
│ ├── settings.py
│ ├── static
│ ├── urls.py
│ └── wsgi.py
├── main.py
啊哈!启动成功。我看到欢迎使用 Django 页面。
但是,当我尝试创建一个用于登录或其他用途的新应用时...
python3 manage.py startapp login
在我现在的 clientFiles 目录中
.
├── asgi.py
├── __init__.py
├── login
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── settings.cpython-37.pyc
│ └── urls.cpython-37.pyc
├── settings.py
├── static
├── urls.py
└── wsgi.py
只需按照此处的教程操作即可:https://docs.djangoproject.com/en/3.1/intro/tutorial01/
我创建了视图,在登录应用程序中创建了 urls.py,并将以下内容插入 clientFiles/urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('login/', include('login.urls')),
path('admin/', admin.site.urls),
]
运行 程序...启动正常。但是当我尝试浏览 http://127.0.0.1:3852 上的任何地方时,我现在得到
ModuleNotFoundError at /
No module named 'login'
Request Method: GET
Request URL: http://127.0.0.1:3852/
Django Version: 3.1.4
Exception Type: ModuleNotFoundError
Exception Value:
No module named 'login'
Exception Location: <frozen importlib._bootstrap>, line 965, in _find_and_load_unlocked
Python Executable: /usr/bin/python3
Python Version: 3.7.3
Python Path:
['/home/user/project',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/user/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Thu, 24 Dec 2020 04:55:22 +0000
这就是我被困的地方。我试过左右修改路径、目录结构、为新目录添加 CherryPy 的额外安装……我不知所措。
如果您有任何想法,我们将不胜感激...或者是否有其他方法可以将网络服务器集成到我的应用程序中,该应用程序能够通过 django 或 Laravel 提供动态和静态内容?
这很容易解决。我只是将我的应用程序的根重新创建到一个 Django 项目中,然后将我的 main.py 复制到该项目中。完成!
我发现 a gist 可以让我将 CherryPy 网络服务器集成到我的程序中,并通过该 CherryPy 网络服务器 运行 一个 Django 项目。但是我在使它正常工作时遇到了一些困难。
这是我代码中的 Class 要点,基本没有变化
class ClientThread(object):
HOST = "127.0.0.1"
PORT = int("3852")
DIR = "/home/user/project/"
STATIC_ROOT = DIR + '/clientFiles/static/'
def mount_static(self, url, root):
"""
:param url: Relative url
:param root: Path to static files root
"""
config = {
'tools.staticdir.on': True,
'tools.staticdir.dir': root,
'tools.expires.on': True,
'tools.expires.secs': 86400
}
cherrypy.tree.mount(None, url, {'/': config})
def run(self):
cherrypy.config.update({
'server.socket_host': self.HOST,
'server.socket_port': self.PORT,
'engine.autoreload_on': False,
'log.screen': True
})
self.mount_static(settings.STATIC_URL, self.STATIC_ROOT)
cherrypy.tree.graft(WSGIHandler())
cherrypy.engine.start()
print("[Info]: Client started. Use http://{0}:{1} to access the WebApp.".format(self.HOST,self.PORT))
cherrypy.engine.block()
def init(self):
print("[Info]: Starting Client WebApp...")
cThread = Thread(target=self.run)
cThread.start()
整个class是通过ClientThread().init()
我的应用程序顶部的要点的前几行(按顺序):
import os
from threading import Timer
os.environ["DJANGO_SETTINGS_MODULE"] = "clientFiles.settings"
import django
django.setup()
import cherrypy
from django.conf import settings
from django.core.handlers.wsgi import WSGIHandler
我的项目布局如下:
.
├── clientFiles
│ ├── clientFiles
│ │ ├── asgi.py
│ │ ├── __init__.py
│ │ ├── __pycache__
│ │ │ ├── __init__.cpython-37.pyc
│ │ │ └── settings.cpython-37.pyc
│ │ ├── settings.py
│ │ ├── urls.py
│ │ └── wsgi.py
│ ├── __init__.py
│ ├── manage.py
│ ├── __pycache__
│ │ └── __init__.cpython-37.pyc
│ └── static
├── main.py
我在 django-admin startproject clientFiles
.
当我尝试 运行 我的程序时,我得到:
Traceback (most recent call last):
File "main.py", line 26, in <module>
django.setup()
File "/usr/local/lib/python3.7/dist-packages/django/__init__.py", line 19, in setup
configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
File "/usr/local/lib/python3.7/dist-packages/django/conf/__init__.py", line 83, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.7/dist-packages/django/conf/__init__.py", line 70, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python3.7/dist-packages/django/conf/__init__.py", line 177, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'clientFiles.settings'
看起来像是模块加载问题,对吧?
所以我尝试将 os.environ["DJANGO_SETTINGS_MODULE"] = "clientFiles.settings"
更改为 os.environ["DJANGO_SETTINGS_MODULE"] = "clientFiles.clientFiles.settings"
因为我的 Django url 在 clientFiles/clientFiles
这样做之后,我的应用程序启动表明 CherryPy 启动没有问题:
[24/Dec/2020:04:37:14] ENGINE Bus STARTING
[24/Dec/2020:04:37:14] ENGINE Started monitor thread 'Autoreloader'.
[24/Dec/2020:04:37:14] ENGINE Serving on http://127.0.0.1:3852
[24/Dec/2020:04:37:14] ENGINE Bus STARTED
[Info]: Client started. Use http://127.0.0.1:3852 to access the WebApp.
但是当我尝试访问位于 http://127.0.0.1:3852 的站点时,我得到:
ModuleNotFoundError at /
No module named 'clientFiles.urls'
Request Method: GET
Request URL: http://127.0.0.1:3852/
Django Version: 3.1.4
Exception Type: ModuleNotFoundError
Exception Value:
No module named 'clientFiles.urls'
Exception Location: <frozen importlib._bootstrap>, line 965, in _find_and_load_unlocked
Python Executable: /usr/bin/python3
Python Version: 3.7.3
Python Path:
['/home/user/project',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/user/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Thu, 24 Dec 2020 04:38:14 +0000
在控制台中我得到:
> Internal Server Error: /
Traceback (most recent call last):
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/exception.py", line 47, in inner
response = get_response(request)
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/base.py", line 165, in _get_response
callback, callback_args, callback_kwargs = self.resolve_request(request)
File "/usr/local/lib/python3.7/dist-packages/django/core/handlers/base.py", line 288, in resolve_request
resolver_match = resolver.resolve(request.path_info)
File "/usr/local/lib/python3.7/dist-packages/django/urls/resolvers.py", line 545, in resolve
for pattern in self.url_patterns:
File "/usr/local/lib/python3.7/dist-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/dist-packages/django/urls/resolvers.py", line 589, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/usr/local/lib/python3.7/dist-packages/django/utils/functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/usr/local/lib/python3.7/dist-packages/django/urls/resolvers.py", line 582, in urlconf_module
return import_module(self.urlconf_name)
File "/usr/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 965, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'clientFiles.urls'
好的,所以目录结构可能有问题....
我将 clientFiles/clientFiles 的内容移动到 clientFiles 中,所以我有这个:
.
├── clientFiles
│ ├── asgi.py
│ ├── __init__.py
│ ├── manage.py
│ ├── __pycache__
│ │ ├── __init__.cpython-37.pyc
│ │ └── settings.cpython-37.pyc
│ ├── settings.py
│ ├── static
│ ├── urls.py
│ └── wsgi.py
├── main.py
啊哈!启动成功。我看到欢迎使用 Django 页面。
但是,当我尝试创建一个用于登录或其他用途的新应用时...
python3 manage.py startapp login
在我现在的 clientFiles 目录中
.
├── asgi.py
├── __init__.py
├── login
│ ├── admin.py
│ ├── apps.py
│ ├── __init__.py
│ ├── migrations
│ │ └── __init__.py
│ ├── models.py
│ ├── tests.py
│ └── views.py
├── manage.py
├── __pycache__
│ ├── __init__.cpython-37.pyc
│ ├── settings.cpython-37.pyc
│ └── urls.cpython-37.pyc
├── settings.py
├── static
├── urls.py
└── wsgi.py
只需按照此处的教程操作即可:https://docs.djangoproject.com/en/3.1/intro/tutorial01/
我创建了视图,在登录应用程序中创建了 urls.py,并将以下内容插入 clientFiles/urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('login/', include('login.urls')),
path('admin/', admin.site.urls),
]
运行 程序...启动正常。但是当我尝试浏览 http://127.0.0.1:3852 上的任何地方时,我现在得到
ModuleNotFoundError at /
No module named 'login'
Request Method: GET
Request URL: http://127.0.0.1:3852/
Django Version: 3.1.4
Exception Type: ModuleNotFoundError
Exception Value:
No module named 'login'
Exception Location: <frozen importlib._bootstrap>, line 965, in _find_and_load_unlocked
Python Executable: /usr/bin/python3
Python Version: 3.7.3
Python Path:
['/home/user/project',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/user/.local/lib/python3.7/site-packages',
'/usr/local/lib/python3.7/dist-packages',
'/usr/lib/python3/dist-packages']
Server time: Thu, 24 Dec 2020 04:55:22 +0000
这就是我被困的地方。我试过左右修改路径、目录结构、为新目录添加 CherryPy 的额外安装……我不知所措。
如果您有任何想法,我们将不胜感激...或者是否有其他方法可以将网络服务器集成到我的应用程序中,该应用程序能够通过 django 或 Laravel 提供动态和静态内容?
这很容易解决。我只是将我的应用程序的根重新创建到一个 Django 项目中,然后将我的 main.py 复制到该项目中。完成!