我正在尝试使用通过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计算机上为OSGeo4W64
安装了GDAL
。
我在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'
]
请帮助!!!
答案 0 :(得分:0)
我能够解决此问题...
这是我所做的:
pyi-makespec dj/manage.py
hiddenimports=['app.urls',],
之类的隐藏导入中添加app.urls pyinstaller manage.spec
最后!!!
答案 1 :(得分:0)
这可能无法解决Django,但解决了我的Plotly地图问题。我将此代码放入.spec文件中,并在通过PyInstaller之后能够运行Plotly图表。无法显示我的地图,并收到错误消息。
File "<frozen importlib._bootstrap>"
ModuleNotFoundError: No module named 'plotly.validators.choropleth' pyinstaller
现在它可以使用以下功能:
# -*- mode: python ; coding: utf-8 -*-
import sys
sys.setrecursionlimit(5000)
block_cipher = None
a = Analysis(['napdgui.py'],
pathex=['C:\\Users\\USERNAME\\Desktop\\Python\\NAPD\\napdguiv1'],
binaries=[],
datas=[('C:\\Users\\USERNAME\\Anaconda3\\Lib\\site-packages\\plotly\\', 'plotly'),
],
hiddenimports=['pkg_resources.py2_warn', 'app.urls'],
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='Tool v08122020',
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='Analysis Tool v081320')