我正在整理一个使用Cygwin可执行文件的Python程序包,而未安装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在包中查找可执行文件?
此信息来自在Windows的命令提示符中运行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
我包含一个安装说明的链接(使用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来“快速”指令以及有关EXE
和DLL
文件的信息。这一切都归功于@benreaves
更简单(但更难看)的解决方法,我将其交给了 一些针对我的Word错误率计算:
在装有Cygwin的笔记本电脑上,我使用
创建sclite.exe
cp src/*/*.c . ; make all
我上一封邮件中的解决方法
我创建了一个包含
sclite.exe
和cygwin1.dll
的zip文件 笔记本电脑的c:/cygwin/bin/
文件夹将该压缩文件发送给她,然后她将两个文件复制到一个新文件中 笔记本电脑
上的文件夹和set PATH=.;%PATH%
我不设置PATH
,因为我希望它是可分发的。
这在运行Windows 7的笔记本电脑上工作得很好,但是她没有 在笔记本电脑上需要Cygwin或任何其他NIST软件。
-本
答案 0 :(得分:0)
我终于有了这个工作。我修补在一起的来源如下。基本的答案是,我使用了__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。查看“注意:”。