gpt4 book ai didi

python - Django Pyinstaller .EXE 给我 ModuleNotFoundError : No module named 'app.urls'

转载 作者:行者123 更新时间:2023-12-01 07:16:10 25 4
gpt4 key购买 nike

我正在尝试使用 Pyinstaller 编译的 EXE 文件来运行 Django 项目。但是,当我使用此命令运行已编译的 .EXE 文件时,我收到 ModuleNotFoundError: No module named 'app.urls' 和另一个 OsError: [WinError 123]。稍后我将解释如何设置这些东西。

这是我运行 .EXE 文件时的完整错误:

C:\Users\omen\Desktop\dist\dj>dj.exe runserver
Watching for file changes with StatReloader
Performing system checks...

Exception in thread django-main-thread:
Traceback (most recent call last):
File "threading.py", line 917, in _bootstrap_inner
File "threading.py", line 865, in run
File "site-packages\django\utils\autoreload.py", line 54, in wrapper
File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 117, in inner_run
self.check(display_num_errors=True)
File "site-packages\django\core\management\base.py", line 390, in check
File "site-packages\django\core\management\base.py", line 377, in _run_checks
File "site-packages\django\core\checks\registry.py", line 72, in run_checks
File "site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
File "site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
File "site-packages\django\utils\functional.py", line 80, in __get__
File "site-packages\django\urls\resolvers.py", line 584, in url_patterns
File "site-packages\django\utils\functional.py", line 80, in __get__
File "site-packages\django\urls\resolvers.py", line 577, in urlconf_module
File "importlib\__init__.py", line 127, in import_module
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "C:\Users\omen\Anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "dj\dj\urls.py", line 22, in <module>
File "site-packages\django\urls\conf.py", line 34, in include
File "importlib\__init__.py", line 127, in import_module
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 'app.urls'

Traceback (most recent call last):
File "dj\manage.py", line 24, in <module>
File "dj\manage.py", line 20, in main
File "site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
File "site-packages\django\core\management\__init__.py", line 375, in execute
File "site-packages\django\core\management\base.py", line 323, in run_from_argv
File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 60, in execute
super().execute(*args, **options)
File "site-packages\django\core\management\base.py", line 364, in execute
File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 95, in handle
self.run(**options)
File "C:\Users\omen\Desktop\dist\dj\django\core\management\commands\runserver.py", line 102, in run
autoreload.run_with_reloader(self.inner_run, **options)
File "site-packages\django\utils\autoreload.py", line 598, in run_with_reloader
File "site-packages\django\utils\autoreload.py", line 583, in start_django
File "site-packages\django\utils\autoreload.py", line 301, in run
File "site-packages\django\utils\autoreload.py", line 307, in run_loop
File "site-packages\django\utils\autoreload.py", line 347, in tick
File "site-packages\django\utils\autoreload.py", line 363, in snapshot_files
File "site-packages\django\utils\autoreload.py", line 262, in watched_files
File "site-packages\django\utils\autoreload.py", line 103, in iter_all_python_module_files
File "site-packages\django\utils\autoreload.py", line 139, in iter_modules_and_files
File "pathlib.py", line 1339, in exists
File "pathlib.py", line 1161, in stat
OSError: [WinError 123] La syntaxe du nom de fichier, de répertoire ou de volume est incorrecte: '<frozen importlib._bootstrap>'
[11168] Failed to execute script manage

首先,我使用以下命令安装了 Pyinstaller 和其他依赖项:

conda install -c conda-forge pyinstaller
conda install django
conda install -c conda-forge gdal
conda install -c anaconda psycopg2

然后,我在 Windows 10 计算机上为 GDAL 安装了 OSGeo4W64

我在settings.py中添加了这个:


import os


if os.name == 'nt':
import platform
OSGEO4W = r"C:\OSGeo4W"
if '64' in platform.architecture()[0]:
OSGEO4W += "64"
assert os.path.isdir(OSGEO4W), "Directory does not exist: " + OSGEO4W
os.environ['OSGEO4W_ROOT'] = OSGEO4W
os.environ['GDAL_DATA'] = OSGEO4W + r"\share\gdal"
os.environ['PROJ_LIB'] = OSGEO4W + r"\share\proj"
os.environ['PATH'] = OSGEO4W + r"\bin;" + os.environ['PATH']

os.environ['DJANGO_SETTINGS_MODULE'] = 'dj.settings'

SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))

TEMPLATE_DIRS = (
os.path.join(SETTINGS_PATH, 'templates'),
)

GEOS_LIBRARY_PATH = r'C:\OSGeo4W64\bin\geos_c.dll'
GDAL_LIBRARY_PATH = r'C:\OSGeo4W64\bin\gdal204.dll'

我还使用此命令生成了一个规范文件:

pyi-makespec dj/manage.py

这是生成的manage.spec:(我在 datas 中添加了 templates 文件夹,但它不起作用...)

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['dj\\manage.py'],
pathex=['C:\\Users\\omen\\Desktop'],
binaries=[],
datas=[('app/templates','app/templates')],
hiddenimports=[],
hookspath=[],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
[],
exclude_binaries=True,
name='manage',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=True )
coll = COLLECT(exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='manage')

然后我使用以下命令生成了项目:

pyinstaller --name=dj dj/manage.py

我还编辑了 libgdal.py 以具有以下内容:

# Custom library path set?
try:
from django.conf import settings
lib_path = settings.GDAL_LIBRARY_PATH
except (AttributeError, ImportError, ImproperlyConfigured, OSError):
lib_path = None

这是我的主要 urls.py:

from django.contrib import admin
from django.urls import path
from django.urls import include, path

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('app.urls')),
]

这是 INSTALLED_APPS:

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app'
]

请帮忙!!!

最佳答案

我能够解决这个问题...

这是我所做的:

  1. 使用 pyi-makespec dj/manage.py 生成规范文件
  2. 在隐藏导入中添加 app.url,例如 hiddenimports=['app.urls',],
  3. 改用此命令pyinstaller manage.spec

终于!!!

关于python - Django Pyinstaller .EXE 给我 ModuleNotFoundError : No module named 'app.urls' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57933562/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com