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

查看:77
本文介绍了如何使用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)导入COM界面,但是编写起来非常麻烦.

@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,许多语言

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函数(本文结尾处我DropBox链接中的embedpython.exe)以及该zip中的所有文件),这可能是您最好,最容易和最快的途径-源代码/用法参考: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]中的依赖库放入您要分发的目录中,您的代码将在不运行的情况下运行麻烦.您没有指定是否要在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天全站免登陆