Importlib ModuleNotFound Error #8449
-
I write the code: #Outside App Running System File#
import os
import subprocess
import importlib
def find_and_run_script(script_name):
"""查找并运行脚本"""
# 获取 _internal/app/py 文件夹的路径
script_dir = "_internal/app"
# 遍历该文件夹中的所有文件
for file_name in os.listdir(script_dir):
# 如果文件名与输入的脚本名匹配
if file_name == script_name + ".py":
# 构建脚本文件的绝对路径
script_path = os.path.join(script_dir, file_name)
# 动态导入脚本
try:
module = importlib.import_module(f"_internal.app.{script_name}")
# 调用脚本中的 start() 函数
module.start()
except ModuleNotFoundError:
print(f"Script '{script_name}' not found.")
except AttributeError:
print(f"Script '{script_name}' does not have a 'start()' function.")
return True
# 如果没有找到匹配的脚本
print(f"No script named '{script_name}.py' found.")
return False
def start():
"""开始函数"""
# 提示用户输入要运行的脚本名
script_name = input("Enter the app name: ")
# 查找并运行脚本
find_and_run_script(script_name)
start() It work in source code,but isn't work in pyinstaller,it has ''Script '{script_name}' not found.'' |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
That's because PyInstaller's import analysis cannot pick up imports of modules with dynamically computed names (or pretty much anything that is not imported using regular And even if it did, it would collect the modules into PYZ archive, so you would not be able to discover them via You will either need to ensure that those script files are collected as data files (but then you will also need to add all their dependencies to hidden imports), or collect them as submodules of |
Beta Was this translation helpful? Give feedback.
-
If Pyinstaller compile .py to .exe,I will going to add .py files to _internal/app/ |
Beta Was this translation helpful? Give feedback.
-
This is my command,does it right? import os
os.system(f'pyinstaller --add-data "bin/_internal/audio:audio"\
--add-data "bin/_internal/font:font"\
--add-data "bin/_internal/image:image"\
--add-data "bin/_internal/app:app"\
--add-data "C:\\Users\\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\\ursina:usina/"\
--add-data "C:\\Users\\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\\ursina-7.0.0.dist-info:ursina-7.0.0.dist-info"\
--add-data "C:\\Users\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\panda3d:panda3d"\
--add-data "C:\\Users\\leonm\PycharmProjects\\leonsystem\\venv\Lib\site-packages\panda3d-1.10.14.dist-info:panda3d-1.10.14.dist-info"\
--add-data "C:\\Users\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\panda3d_tools:panda3d_tools"\
--add-data "C:\\Users\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\direct:direct"\
--add-data "C:\\Users\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\pipeline:pipeline"\
--add-data "C:\\Users\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\pipeline-0.1.0.dist-info:pipeline-0.1.0.dist-info"\
--path "C:\\Users\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\panda3d"\
--path "C:\\Users\\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\\ursina"\
--path "C:\\Users\\leonm\PycharmProjects\leonsystem\\venv\Lib\site-packages\\ursina-7.0.0.dist-info"\
--path "C:\\Users\\leonm\PycharmProjects\\leonsystem\\venv\Lib\site-packages\panda3d-1.10.14.dist-info"\
--collect-submodules panda3d --collect-binaries panda3d --collect-data panda3d --collect-data ursina --collect-submodules ursina --collect-binaries ursina --collect-data panda3d-1.10.14.dist-info --collect-submodules panda3d-1.10.14.dist-info --collect-binaries panda3d-1.10.14.dist-info --collect-data ursina-7.0.0.dist-info --collect-submodules ursina-7.0.0.dist-info --collect-binaries ursina-7.0.0.dist-info\
--hidden-import _internal.app\
-i="C:\\Users\\leonm\\PycharmProjects\\leonsystem\\icon.ico" \
-D -y \
C:\\Users\leonm\PycharmProjects\leonsystem\\bin\cmd.py') |
Beta Was this translation helpful? Give feedback.
Actually, this is likely the culprit:
In the frozen application, the module should be called
app.{script_name}
, since_internal
directory is in the search path (as opposed to its parent).