使用Python软件包和__file__ import设置VENV中的Pyinstaller的Spec文件

发布于 2025-02-03 12:46:03 字数 1220 浏览 3 评论 0原文

问题1:我决定将我的程序重组为包装,这是文件结构:

venv

- < program_folder>

-__init__.py
-GUI.py
-<pictures_folder>
    -icon.png
-<modules_folder>
    -__init__.py
    -a.py
    -b.py

GUI.PY同时进口A.Py和B.Py(以及其他标准库)。我无法使用以下方式导入这些模块:

from modules_folder import a.py, b.py

大概是因为我使用虚拟环境来运行代码。因此,按照在线建议,我使用以下代码将模块目录添加到路径中(此代码正常):

d = dirname(abspath(__file__)) + '\\modules_folder'
sys.path.insert(1,d)
import a.py, b.py 

我正在尝试使用命令“ Pyinstaller Gui.spec”在我的VENV下进行.exe,但是我的模块是不包括。目前尚不清楚如何使用挂钩,Pathex,hiddenImports,Hookpath等正确添加这些文件。我已经尝试将模块目录添加到Spec文件中的每个变量中。正确的方法是什么?

a = Analysis(
['GUI.py'],
pathex=['C:\\Users\\.\\Lib\\site-packages'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=['C:\\.\\modules_folder'],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,

问题2:无法使用- OneFile-Noconsole命令。这是否意味着任何.EXE都需要一个控制台?

环境:

  • Windows-10-10
  • Python:3.10.4
  • Pyinstaller:5.0.1

Problem 1: I decided to reorganize my program into a package and this is the file structure:

venv

-<program_folder>

-__init__.py
-GUI.py
-<pictures_folder>
    -icon.png
-<modules_folder>
    -__init__.py
    -a.py
    -b.py

The GUI.py imports both a.py and b.py (along with other standard libraries). I was unable to import these modules using:

from modules_folder import a.py, b.py

Presumably because I was using a virtual environment to run the code. So, following online advice, I added the module directory to the path using the following code (this code works properly):

d = dirname(abspath(__file__)) + '\\modules_folder'
sys.path.insert(1,d)
import a.py, b.py 

I'm trying to make the .exe under my venv using the command 'pyinstaller GUI.spec' but my modules are not included. It's unclear how to properly add these files using either hooks, pathex, hiddenimports, hookpath etc. I've already tried to add the modules directory to each variable in the spec file. What's the correct way to do this?

a = Analysis(
['GUI.py'],
pathex=['C:\\Users\\.\\Lib\\site-packages'],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=['C:\\.\\modules_folder'],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,

Problem 2: Cannot use --onefile or --noconsole command. Does this mean any .exe will necessitate a console?

Environment:

  • Windows-10-10
  • Python: 3.10.4
  • PyInstaller: 5.0.1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光无声 2025-02-10 12:46:04

因此,我发布的示例实际上工作了。

问题是一个模块导入其他软件包。我只是将这些导入添加到main.py文件中,以确保Pyinstaller发现它们。另外,我可能可以将它们添加到.spec文件中的HiddenImports中,但这无效,因此我待在简单的解决方案中。

So the example I posted actually worked.

The issue was one module imports other packages. I just added these imports to the main.py file to make sure they were found by pyinstaller. Alternatively I probably could add them to hiddenimports in the .spec file but this didn't work so I stayed with the simple solution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文