具有相对导入的 Pyinstaller [英] Pyinstaller with relative imports

查看:74
本文介绍了具有相对导入的 Pyinstaller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Pyinstaller 2.1 为 Windows 7 构建 Pyinstaller.该模块使用相对导入,因为该包通常在 Linux 中用作常规"Python 包.有没有办法创建规范文件来处理这种类型的设置?理想情况下,我想要一个 Python 包,我可以使用它为 Windows 制作 Pyinstaller exe,并在 Linux/OS X 中拥有一个常规"的可通过 pip 安装的 Python 包.

I'm attempting to make a Pyinstaller build for Windows 7 using Pyinstaller 2.1. The module uses relative imports because the package is typically used in Linux as a 'regular' Python package. Is there a way to create a spec file to handle this type of setup? Ideally I would like to have a Python package that I can make a Pyinstaller exe with for Windows and have a 'regular' pip-installable Python package in Linux/OS X.

我正在考虑使用隐藏导入或其他方法来实现这一点.

I was thinking of maybe using hidden imports or something to achieve this.

我尝试使用默认的 Pyinstaller 设置并将其指向我的主要"python 脚本.我从生成的 exe 中得到以下内容:

I've tried using the default Pyinstaller settings and pointing it at my 'main' python script. I get the following from the resulting exe:

'在非包中尝试相对导入'

'Attempted relative import in non-package'

这是有道理的,因为我将 Pyinstaller 指向包中的 main.py 文件,而 Pyinstaller 并没有安装我的整个包.这只是从命令行使用模块的起点.但是,您也可以导入它并在您自己的代码中使用它.

This makes sense because I'm pointing Pyinstaller at my main.py file in the package and Pyinstaller is NOT picking up my entire package. This is just the starting point for using the module from the command-line. However, you can import it and use it in your own code as well.

旁注:

原因是这个包需要 numpy 和 scipy.是的,我知道有一些很好的方法可以让这些在 Windows 中使用 Anaconda 等工作.但是,由于遗留原因,我现在坚持使用 exe 设置.

The reasoning is this package requires numpy and scipy. Yes, I know there are good ways to get these working in Windows with Anaconda, etc. However, I'm stuck with an exe setup now for legacy reasons.

推荐答案

我找不到让 Pyinstaller 执行此操作的方法.但是,我不认为这是 Pyinstaller 的错.更像是我构建包的方式的问题.

I can't find a way to get Pyinstaller to do this. However, I don't think it's the fault of Pyinstaller. It's more of a problem with the way I structured my package.

我将一个脚本传递给 Pyinstaller,它是我的包的一部分.更好的方法是在包外提供一个简单的 Python 脚本,作为包的 cli 前端.

I was passing a script to Pyinstaller that was a part of my package. The better way to do that would be to provide a simple Python script outside of the package that serves as the cli front-end to the package.

例如,考虑这样的包布局(假设文件使用相对导入):

For example, consider a package layout like this (assume files use relative imports):

repo_dir/
    setup.py
    README.md
    package_a/
        main.py
        support_module.py
        __init__.py

我之前的尝试是尝试通过将 main.py 传递给 Pyinstaller 来构建它.这导致了上述问题中提到的错误.

My previous attempt was trying to build main.py by passing it to Pyinstaller. This resulted in the error mentioned in the above question.

但是,我后来添加了一个 cli.py 脚本,它执行如下操作:

However, I later added a cli.py script that does something like this:

from package_a.main import main
if __name__ == '__main__':
    main()

现在,我可以将 cli.py 传递给 Pyinstaller,并且我的显式相对导入仅在包内部使用.所以,这一切都有效.这是一个示例目录布局,仅供参考:

Now, I can pass cli.py to Pyinstaller and my explicit relative imports are only used inside of the package. So, it all works. Here's a sample directory layout that works just for reference:

repo_dir/
    setup.py
    cli.py
    README.md
    package_a/
        main.py
        support_module.py
        __init__.py

这篇关于具有相对导入的 Pyinstaller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆