如何使用 Python 创建独立于语言的库? [英] How can I create a language independent library using Python?

查看:28
本文介绍了如何使用 Python 创建独立于语言的库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 Python 中创建了一个包,那么另一个 Python 用户可以导入该包并与之交互.

If I create a package in Python, then another Python user can import that package and interface with it.

如何创建一个包,以便其他用户调用库的语言无关紧要?

How can I create a package so that it doesn't matter what language the other user is calling the library with?

我可以指定输入和输出文件格式,以便另一种语言可以通过仅提供输入文件和读取输出文件来与我的 Python 代码交互.但是,创建输入和输出文件在计算上非常昂贵.有没有更简单的解决方案?

I could specify input and output file formats so that another language can interface with my Python code by merely providing the input files and reading the output files. However, creating the input and output files is very expensive computationally. Is there an easier solution?

推荐答案

@bluprince13 没有这样的方法可以从每种语言中调用库,至少在没有包装代码的情况下不能直接调用.Windows 上的 COM 接口很接近,然后可以被大多数程序(例如 Excel、MATLAB、JAVA)导入,但是编写起来非常痛苦.

@bluprince13 There is no such way to have a library callable from every language, at least not directly without wrapper code. COM interface on Windows is close which then can be imported by most programs (such as Excel, MATLAB, JAVA) but this is a huge pain to write.

当您说读/写是一项昂贵的操作时,您一定不能使用 Pandas read_csvto_csv 函数 - 它们是极快的 (C++) 实现.更快的是二进制 HDF5 文件,尽管它们对于大多数用户来说更难使用 http://pandas.pydata.org/pandas-docs/version/0.20/io.html read_hdfto_hdf,支持多种语言https://en.wikipedia.org/wiki/Hierarchical_Data_Format.使用输入和输出文件将使您的程序更具可移植性.

When you say the read/write is an expensive operation, you must not be using Pandas read_csv and to_csv functions - they are blazing fast (C++) implementations. Faster yet are binary HDF5 files although they are harder to work with for most users http://pandas.pydata.org/pandas-docs/version/0.20/io.html read_hdf and to_hdf, which is supported by plenty of languages https://en.wikipedia.org/wiki/Hierarchical_Data_Format. Using input and output files will make your program more portable.

使用嵌入式 Python(已编译),您可以简单地使用您以 .py 形式创建的任何 Python 函数(embedpython.exe 在最后的 DropBox 链接中)这篇文章的全部内容,以及 zip 中的所有文件),这可能是您最好、最简单和最快的途径——源代码/使用参考:嵌入式 Python 无法使用 NumPy 指向 Python35.zip - 如何修复?到目前为止,这是让您的代码在任何系统上兼容的最简单方法,并且在您的 Python 脚本之间进行更改也很容易(当您调用不同的库时,整个包必须在子文件夹中可用).在 Anaconda Python 安装中,文件将位于各自安装的包文件夹中,通常是 C:\Anaconda3\Lib\site-packages\ [packageName] \ ;典型的 Python 安装位于 C:\Python\Lib\site-packages\ [packageName] \).否则,从安装 Python 的命令提示符 cd\ 中,dir/s site-packages 将找到该位置.然后将每个包的整个安装目录放在extension_modules"目录下.所以它看起来像 extension_modules\numpy\extension_modules\pandas\,以及你正在导入的所有库(以及包所依赖的库).

Using embedded Python (compiled) you can simply use whatever Python functions you've created in their .py form (embedpython.exe at my DropBox link at the end of this post, along with all the files in the zip there), which is probably your best, easiest, and fastest route- for sourcecode / usage reference: Embedded Python does not work pointing to Python35.zip with NumPy - how to fix? It is by FAR the easiest way to get your code compatible on any system, and changing between your Python scripts is easy (when you are calling different libraries the entire packages have to be available in a subfolder). In an Anaconda Python installation the files will be in the respective installed packages folder, usually C:\Anaconda3\Lib\site-packages\ [packageName] \ ; typical Python installations are located at C:\Python\Lib\site-packages\ [packageName] \). Otherwise from a command prompt cd\ where Python is installed, then dir /s site-packages will find the location. Then you put the entire installation directory for each package under the "extension_modules" directory. So it looks like extension_modules\numpy\, extension_modules\pandas\, and all the libraries you are importing (along with libraries the packages are dependent on).

以下是一些如何使用 EXE 调用相应语言的示例: JAVA: Process process = new ProcessBuilder("C:\\PathToExe\\embedpython.exe","pyscript","pyfunction","input_param1","input_param2").start(); MATLAB: system('"C:\PathToExe\embedpython.exe" pyscript pyfunction input_param1 input_param2');VBA: Call Shell("C:\PathToExe\embedpython.exe" "pyscript" "pyfunction" "param1" "param2", vbNormalFocus) C++: 如何使用参数调用外部程序? .NET C#:如何将参数传递给 exe? 如您所见,这个列表还在继续……几乎所有语言可以调用一个EXE文件.当然,您想要最大的性能,但要获得所有语言的兼容性,您必须以某种方式妥协.但是使用上述建议,如果 .py 功能得到优化,您的性能应该仍然很好.

Here are some examples of how to call the respective language with the EXE: JAVA: Process process = new ProcessBuilder("C:\\PathToExe\\embedpython.exe","pyscript","pyfunction","input_param1","input_param2").start(); MATLAB: system('"C:\PathToExe\embedpython.exe" pyscript pyfunction input_param1 input_param2'); VBA: Call Shell("C:\PathToExe\embedpython.exe" "pyscript" "pyfunction" "param1" "param2", vbNormalFocus) C++: How to call an external program with parameters? .NET C#: How to pass parameters to an exe? as you see, the list goes on and on... pretty much any language can call an EXE file. Sure you want maximum performance but to get compatibility across all languages you have to compromise in some fashion. But using the above suggestions your performance should still be great provided the .py functions are optimized.

让每个人的生活更轻松这里是编译版本 x64 Python 3.5 Windows NumPy SciPy 和 Pandas Intel MKL 包括:https://www.dropbox.com/sh/2smbgen2i9ilf2e/AADI8A3pCAFU-EqNLTbOiUwJa?dl=0

Making everyone's life easier here's the compiled version x64 Python 3.5 Windows NumPy SciPy and Pandas Intel MKL included: https://www.dropbox.com/sh/2smbgen2i9ilf2e/AADI8A3pCAFU-EqNLTbOiUwJa?dl=0

如果您是 Windows 用户,请下载上述内容并将您的 .py 脚本放在您要分发的位置,以及 \extension_modules\ [package_name] 中的依赖库,然后您're 代码将轻松运行.您没有指定这是否要在 Linux 下使用,因此这是我对从任何语言使用"问题的 Windows 解决方案,需要对其他编程语言有最少的了解.

If you are a Windows user, download the above and put your .py script in there you'd like to distribute, along with the dependent libraries in the \extension_modules\ [package_name] and you're code will run without hassle. You didn't specify if this is to be used under Linux so that's my Windows solution to your "use from any language" question requiring the least bit of knowledge of other programming languages.

这篇关于如何使用 Python 创建独立于语言的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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