Django:为什么在虚拟环境中找不到软件包?

时间:2020-02-24 21:00:12

标签: python django python-3.x elasticsearch

我目前正在使用弹性搜索和干草堆软件包在django中建立一个简单的搜索。

但是,当我运行服务器时,发生以下错误:

(development_env) C:\Users\user\Desktop\Development\development>python manage.py runserver
Watching for file changes with StatReloader
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\threading.py", line 932, in _bootstrap_inner
    self.run()
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\apps\registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\apps\config.py", line 90, in create
    module = import_module(entry)
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'elasticsearch-dsl'
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\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\commands\runserver.py", line 60, in execute
    super().execute(*args, **options)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\commands\runserver.py", line 95, in handle
    self.run(**options)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\core\management\commands\runserver.py", line 102, in run
    autoreload.run_with_reloader(self.inner_run, **options)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 598, in run_with_reloader
    start_django(reloader, main_func, *args, **kwargs)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 583, in start_django
    reloader.run(django_main_thread)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 301, in run
    self.run_loop()
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 307, in run_loop
    next(ticker)
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 347, in tick
    for filepath, mtime in self.snapshot_files():
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 363, in snapshot_files
    for file in self.watched_files():
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 262, in watched_files
    yield from iter_all_python_module_files()
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 103, in iter_all_python_module_files
    return iter_modules_and_files(modules, frozenset(_error_files))
  File "C:\Users\user\Desktop\Development\development\development_env\lib\site-packages\django\utils\autoreload.py", line 139, in iter_modules_and_files
    if not path.exists():
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 1385, in exists
    self.stat()
  File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\pathlib.py", line 1191, in stat
    return self._accessor.stat(self)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: '<frozen importlib._bootstrap>'

我特别想知道该错误:ModuleNotFoundError:没有名为“ elasticsearch-dsl”的模块。 如果我检查已安装的软件包,会看到已安装“ elasticsearch-dsl”:

(development_env) C:\Users\user\Desktop\Development\development>pip list
Package                  Version
------------------------ -------
appdirs                  1.4.3
asgiref                  3.2.3
distlib                  0.3.0
Django                   2.2.10
django-crispy-forms      1.8.1
django-elasticsearch-dsl 7.1.1
django-haystack          2.8.1
django-six               1.0.4
elasticsearch            7.5.1
elasticsearch-dsl        7.1.0
filelock                 3.0.12
Pillow                   7.0.0
pip                      19.2.3
python-dateutil          2.8.1
pytz                     2019.3
setuptools               41.2.0
six                      1.14.0
sqlparse                 0.3.0
urllib3                  1.25.8
virtualenv               20.0.5
virtualenvwrapper-win    1.2.6

我还检查了'django-elasticsearch-dsl'而不是'elasticsearch-dsl',但错误代码相同。我似乎在我的恶劣环境中无法正确安装弹性搜索包。

我很感谢每一个提示。

丹尼尔问候

1 个答案:

答案 0 :(得分:0)

你必须在你的 app/settings.py 中添加这个包:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    ....
    'elasticsearch-dsl'
]

错误应该会消失。

问候亚历克斯