如何使python包引用打包的非python可执行文件(.exe);Windows

2024-06-16 10:45:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个Python包,它使用Cygwin可执行文件,而没有安装Cygwin。这意味着我的包中有一个可执行(.exe)文件和一个库(.dll)文件。我这样做是为了让只使用Windows和Python的人可以在Windows上使用这个工具。我是Python软件包的新手,所以非常感谢您的帮助。你知道吗


主要问题

如何使包指向包中的可执行文件?例如,可以使用它来代替PATH引用的可执行文件。答案可能就在那里,但我在搜索中找到的只是一堆关于如何从Python脚本创建可执行文件的信息,这不是我想要的。你知道吗


详细信息/我的尝试

这个工具是sclite,一个为语音识别输出评分的工具。有关如何在Windows上获得该工具的信息,请查看下面的如何安装SCLITE(用于复制能力)部分。你知道吗

这里有一个小的“玩具”的例子,说明我如何设置我的包。你知道吗

$ tree package_holder_dir/
package_holder_dir/
├── bbd_package
│   ├── __init__.py
│   ├── non_py_exe_dll
│   │   ├── cygwin1.dll
│   │   └── sclite.exe
│   ├── score
│   │   ├── __init__.py
│   │   └── run_sclite.py
│   └── score_transcript.py
├── MANIFEST.in
├── README.txt
└── setup.py

3 directories, 9 files

我的第一个猜测是sclite.exe应该放在MANIFEST.in文件中。你知道吗

# @file MANIFEST.in

include ./README.txt
include ./bbd_package/non_py_exe_dll/sclite.exe
include ./bbd_package/non_py_exe_dll/cygwin1.dll

我也试着把它们放在setup.py

我的setup.py如下

#!/usr/bin/env/python3
# -*- coding: utf-8 -*-
# @file setup.py

import setuptools
from distutils.core import setup

with open ("README.txt", "r") as fh:
  long_description = fh.read()

setup(
  name='bbd_package',
  url='user@host.com',
  author='bballdave025',
  author_email='not.likely@idontwantspam.com',
  packages=setuptools.find_packages(),
  #data_files=[('lib', ['./non_py_exe_dll/cygwin1.dll']),
  #                     './non_py_exe_dll/sclite.exe'],
  version='0.0.1',
  description="Example for SO",
  long_description=long_description,
  include_package_data=True
) ##endof:  setup

注意this source,我使用data_files作为DLL的位置。但是,我在别处安装发行版时找不到这些文件。这就是为什么他们在这里被评论。你知道吗

使用MANIFEST.in似乎是可行的,但是我必须使用相对路径来访问可执行文件。当尝试在另一个目录中import bbd_package时,这将不起作用。你知道吗

让我试着用我的两个Python文件来说明:

score_transcript.py只是调用run_sclite.py。你知道吗

#!/usr/env/bin python3
# -*- coding: utf-8 -*-
# @file run_sclite.py

import os, sys, subprocess

def run_sclite(hyp, ref):
  subprocess.call(['../non_py_exe_dll/sclite.exe', '-h', hyp, '-r', ref, '-i', 'rm', \
                   '-o', 'all snt'])

我可以在我的系统上安装它:

C:\toy_executable_example\package_holder_dir>pip install .

如果我正好在run_sclite.py目录中

C:\toy_executable_example\package_holder_dir\bbd_package\score>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import bbd_package.score.run_sclite
>>> bbd_package.score.run_sclite.run_sclite('a.hyp', 'a.ref')
sclite Version: 2.10, SCTK Version: 1.3
...output that shows it works...
>>>

但是,从任何其他目录,没有骰子。你知道吗

C:\Users\me>python
Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 17:00:18) [MSC v.1900 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import bbd_package.score.run_sclite
>>> bbd_package.score.run_sclite.run_sclite('a.hyp', 'a.ref')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\dblack\AppData\Local\Programs\Python\Python36\lib\site-packages\bbd_package\score\run_sclite.py", line 9, in run_sclite
    '-o', 'all snt'])
  File "C:\Users\dblack\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 267, in call
    with Popen(*popenargs, **kwargs) as p:
  File "C:\Users\dblack\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 709, in __init__
    restore_signals, start_new_session)
  File "C:\Users\dblack\AppData\Local\Programs\Python\Python36\lib\subprocess.py", line 997, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
>>>

如何让Python在包中查找可执行文件?


系统详细信息

此信息来自于从窗口的命令提示符运行systeminfo。你知道吗

OS Name:                   Microsoft Windows 10 Enterprise
OS Version:                10.0.15063 N/A Build 15063
OS Manufacturer:           Microsoft Corporation
OS Configuration:          Member Workstation
OS Build Type:             Multiprocessor Free

如何安装SCLITE(用于复制能力)

我包含了一个指向安装说明的链接(使用Cygwin)here,请查看我的评论。这些命令没有解释

$ cd
$ git clone https://github.com/kaldi-asr/kaldi.git
$ cd kaldi/tools
$ extras/check_dependencies.sh
$ make -j $(nproc --all)
$ cp -R sctk-2.4.10 ~/
$ cd
$ rm -rf kaldi
$ cd sctk-2.4.10/
$ cp $HOME/.bashrc "${HOME}/.bashrc.$(date +%Y%m%d-%H%M%S).bak"
$ echo -e "\n\n## Allow access to sclite, rfilter, etc" >> $HOME/.bashrc
$ echo 'export PATH='"$(pwd)/bin"':$PATH' >> $HOME/.bashrc
$ source ~/.bashrc

我还包含了一个link到“更快”的指令以及关于EXEDLL文件的信息。这都要感谢@benreaves

Easier (but even uglier) workaround, which I gave to an intern who did some Word Error Rate calculations for me:

On my laptop with Cygwin installed, I create sclite.exe using my

cp src/*/*.c . ; make all

workaround from my previous message

I create a zip file containing sclite.exe and cygwin1.dll from my laptop's c:/cygwin/bin/ folder

Email that zip file to her, and she copies both files in a single new folder on her laptop and set PATH=.;%PATH%

我不设置PATH,因为我希望它是可分配的。你知道吗

This worked just fine on her laptop running Windows 7, and she didn't need Cygwin or any other NIST software on her laptop.

-Ben


Tags: 文件runinpyimport可执行文件packagesetup
1条回答
网友
1楼 · 发布于 2024-06-16 10:45:47

我终于成功了。下面是我拼凑的资料来源。基本答案是,我使用__file__变量来查找调用打包模块的目录,然后使用相对路径来获取可执行文件。我对这个解决方案不是很满意(here在某些情况下它不起作用),但它现在完成了任务。你知道吗

细节

MANIFEST.in文件保持不变:

# @file MANIFEST.in
# @author bballdave025

include ./README.txt
include ./bbd_package/non_py_exe_dll/sclite.exe
include ./bbd_package/non_py_exe_dll/cygwin1.dll

但是,我需要在exe运行的代码中添加以下内容。这是在run_sclite.py

from pathlib import Path
#...
path_to_here = os.path.dirname(os.path.abspath(__file__))
path_to_pkg_root = Path(path_to_here).resolve().parents[1]
path_to_exe = os.path.join(path_to_pkg_root, 'non_py_exe_dll')
#...

请注意,pathlib需要Python版本>;=3.4

以下是整个(run_sclite.py)文件:

#!/usr/env/bin python3
# -*- coding: utf-8 -*-
# @file run_sclite.py
# @author bballdave025

import os, sys, subprocess
from pathlib import Path

def run_sclite(hyp, ref):
  path_to_here = os.path.dirname(os.path.abspath(__file__))
  path_to_pkg_root = Path(path_to_here).resolve().parents[1]
  path_to_exe = os.path.join(path_to_pkg_root, 'non_py_exe_dll')

  subprocess.call([os.path.join(path_to_exe, 'sclite.exe'), '-h', hyp, '-r', ref, \
                   '-i', 'rm', '-o', 'all snt'])

##endof:  run_sclite(hyp, ref)

来源

Mouse vs. Python,并澄清此解决方案何时不起作用以及其他替代方案。你知道吗

Getting to package root directory (SO)

是什么让我最终得到了答案:Python Packaging Tutorial。请看“注意:”。你知道吗

相关问题 更多 >