Django python manage.py runserver 给出异常数据文件未找到,数据文件生成失败

Django python manage.py runserver giving exception Datafile not found, datafile generation failed

升级我的一个包(django-registration)后,我在尝试 运行 python manage.py runserver

时收到此错误

输出如下所示。

我以为

python3.5/site-packages/confusable_homoglyphs/categories.py

可能丢失了,我重新安装了软件包 confusable_homoglyphs,但没有用。它抛出了同样的错误

还有之后,

在我的项目文件夹中创建了一个文件名 =3.0.0.txt,它写在下面

Requirement already satisfied: confusable_homoglyphs in ./myvenv/lib/python3.5/site-packages

我试图删除该包并添加该包(即使在我的虚拟环境之外的 n 内)。但它抛出了与下面相同的错误。

(myvenv) shubhendu@shubhendu-HP-Pavilion-g6-Notebook-PC:/home/foodballbear$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7f320538d400>
Traceback (most recent call last):
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/confusable_homoglyphs/categories.py", line 141, in <module>
    categories_data = load('categories.json')
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/confusable_homoglyphs/utils.py", line 33, in load
    with open('{}/{}'.format(os.getcwd(), filename), 'r') as file:
FileNotFoundError: [Errno 2] No such file or directory: '/home/foodballbear/categories.json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/confusable_homoglyphs/categories.py", line 144, in <module>
    if generate():
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/confusable_homoglyphs/categories.py", line 111, in generate
    file = get(url)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/confusable_homoglyphs/utils.py", line 24, in get
    return urlopen(url).read().decode('utf-8').split('\n')
  File "/usr/lib/python3.5/http/client.py", line 461, in read
    s = self._safe_read(self.length)
  File "/usr/lib/python3.5/http/client.py", line 607, in _safe_read
    chunk = self.fp.read(min(amt, MAXAMOUNT))
  File "/usr/lib/python3.5/socket.py", line 575, in readinto
    return self._sock.recv_into(b)
ConnectionResetError: [Errno 104] Connection reset by peer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/utils/autoreload.py", line 227, in wrapper
    fn(*args, **kwargs)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/foodballbear/myfoodsite/urls.py", line 27, in <module>
    url(r'^accounts/', include('registration.backends.simple.urls')),
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/django/conf/urls/__init__.py", line 50, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/registration/backends/simple/urls.py", line 10, in <module>
    from . import views
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/registration/backends/simple/views.py", line 10, in <module>
    from registration.views import RegistrationView as BaseRegistrationView
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/registration/views.py", line 12, in <module>
    from registration.forms import RegistrationForm
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/registration/forms.py", line 19, in <module>
    from . import validators
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/registration/validators.py", line 10, in <module>
    from confusable_homoglyphs import confusables
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/confusable_homoglyphs/confusables.py", line 7, in <module>
    from .categories import unique_aliases, alias
  File "/home/foodballbear/myvenv/lib/python3.5/site-packages/confusable_homoglyphs/categories.py", line 147, in <module>
    raise Exception('Datafile not found, datafile generation failed!')
Exception: Datafile not found, datafile generation failed!

您正试图在 Django 中访问 categories.json 此文件您无法直接访问该文件。由于发生异常。

使用以下解决方案访问 categories.json

from project_name.settings import BASE_DIR
import os

file_path = os.path.join(BASE_DIR, 'categories.json')
categories_data = load(file_path)

BASE_DIR 给你你的项目目录路径,然后你想使用 os.path.join()

加入你的文件路径

希望对您有所帮助

此问题已在

之前提出

https://github.com/vhf/confusable_homoglyphs/issues/7

我现在可以通过 manage.py 运行 我的服务器了,似乎包有问题

confusable_homoglyphs

它会在加载时下载文件。当依赖项经常被下载时,它会产生问题,这会导致

ConnectionResetError: [Errno 104] Connection reset by peer

感谢评论中提供的link,补丁已经应用并且可以很好地与confusable_homoglyphs>=3.0.0.

一起使用

运行 带有选项 --no-site-packages 的 virtualenv 将不包括全局安装的包。它可能有助于保持包列表干净,以防以后需要访问它

为了保持环境一致,我“冻结”了环境包的当前状态

$ pip freeze > requirements.txt

这是我的requirements.txt

Django==1.11.1
django-dajax==0.9.2
django-dajaxice==0.7
django-filter==1.1.0
django-redis==4.8.0
djangorestframework==3.6.3
pkg-resources==0.0.0
psycopg2==2.7.3.2
pytz==2017.2
redis==2.10.6

这是我添加`

后的requirements.txt

confusable_homoglyphs>=3.0.0

` 所需的版本

Django==1.11.1
django-dajax==0.9.2
django-dajaxice==0.7
django-filter==1.1.0
django-redis==4.8.0
djangorestframework==3.6.3
pkg-resources==0.0.0
psycopg2==2.7.3.2
pytz==2017.2
redis==2.10.6
confusable_homoglyphs>=3.0.0

那我运行pip install -r requirements.txt

(myvenv) shubhendu@shubhendu-HP-Pavilion-g6-Notebook-PC:/home/foodballbear  :  pip install -r requirements.txt 
Obtaining bootstrap from git+git://github.com/earle/django-bootstrap.git@97eb50b3f4932798e7fa8ad4b42f0534d521826d#egg=bootstrap (from -r requirements.txt (line 1))
  Skipping because already up-to-date.
Requirement already satisfied: Django==1.11.1 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 2))
Requirement already satisfied: django-dajax==0.9.2 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 3))
Requirement already satisfied: django-dajaxice==0.7 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 4))
Requirement already satisfied: django-filter==1.1.0 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 5))
Requirement already satisfied: django-redis==4.8.0 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 6))
Requirement already satisfied: djangorestframework==3.6.3 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 7))
Requirement already satisfied: pkg-resources==0.0.0 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 8))
Requirement already satisfied: psycopg2==2.7.3.2 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 9))
Requirement already satisfied: pytz==2017.2 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 10))
Requirement already satisfied: redis==2.10.6 in ./myvenv/lib/python3.5/site-packages (from -r requirements.txt (line 11))
Collecting confusable_homoglyphs>=3.0.0 (from -r requirements.txt (line 12))
  Downloading confusable_homoglyphs-3.0.0-py2.py3-none-any.whl (173kB)
    100% |████████████████████████████████| 174kB 828kB/s 
Installing collected packages: bootstrap, confusable-homoglyphs
  Found existing installation: bootstrap 0.1.0
    Uninstalling bootstrap-0.1.0:
      Successfully uninstalled bootstrap-0.1.0
  Running setup.py develop for bootstrap
  Found existing installation: confusable-homoglyphs 2.0.2
    Uninstalling confusable-homoglyphs-2.0.2:
      Successfully uninstalled confusable-homoglyphs-2.0.2
Successfully installed bootstrap confusable-homoglyphs-3.0.0

在那之后我的 python manage.py runserver 没有抛出任何错误

(myvenv) shubhendu@shubhendu-HP-Pavilion-g6-Notebook-PC:/home/foodballbear$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
December 12, 2017 - 06:08:24
Django version 1.11.1, using settings 'myfoodsite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.