gpt4 book ai didi

python - 使用 PEX 将 python 脚本与 pandas 打包

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:27 24 4
gpt4 key购买 nike

我有一个依赖于 pandas 的简单 python 脚本。我需要用 pex 打包它,这样它就可以在没有依赖安装的情况下执行。

import sys
import csv
import argparse
import pandas as pd

class myLogic():
def __init__(self):
pass

def loadData(self, data_file):
return pd.read_csv(data_file, delimiter="|")

#command line interaction interface
def processInputArguments(self,args):

parser = argparse.ArgumentParser(description="my logic")

#transactions file name
parser.add_argument('-td',
'--data',
type=str,
dest='data',
help='data file location'
)


options = parser.parse_args(args)
return vars(options)


def main(self):
options = self.processInputArguments(sys.argv[1:])

data_file = options["data"]

data = self.loadData(data_file)
print data.head()


if __name__ == '__main__':
ml = myLogic()
ml.main()

我正在尝试使用 pex 来执行此操作,因此我执行了以下操作:

pex pandas -e myprogram.myLogic:main -o test1.pex 

但是在运行生成的 pex 文件时出现此错误:

Traceback (most recent call last):
File ".bootstrap/_pex/pex.py", line 317, in execute
File ".bootstrap/_pex/pex.py", line 250, in _wrap_coverage
File ".bootstrap/_pex/pex.py", line 282, in _wrap_profiling
File ".bootstrap/_pex/pex.py", line 360, in _execute
File ".bootstrap/_pex/pex.py", line 418, in execute_entry
File ".bootstrap/_pex/pex.py", line 435, in execute_pkg_resources
File ".bootstrap/pkg_resources.py", line 2088, in load
ImportError: No module named myLogic

我还尝试使用以下命令使用 -c(脚本开关)进行打包:

pex pandas -c myprogram.py -o test2.pex

但也出现错误:

Traceback (most recent call last):
File "/usr/local/bin/pex", line 11, in <module>
sys.exit(main())
File "/usr/local/lib/python2.7/dist-packages/pex/bin/pex.py", line 509, in main
pex_builder = build_pex(reqs, options, resolver_options_builder)
File "/usr/local/lib/python2.7/dist-packages/pex/bin/pex.py", line 486, in build_pex
pex_builder.set_script(options.script)
File "/usr/local/lib/python2.7/dist-packages/pex/pex_builder.py", line 214, in set_script
script, ', '.join(self._distributions)))
TypeError: sequence item 0: expected string, DistInfoDistribution found

最佳答案

到目前为止,对我有用的唯一选择是使用包含 pandas 的 pex 创建解释器,然后将其与 python 脚本一起发送。这可以按如下方式完成:

pex pandas -o my_interpreter.pex

但是当构建 python 版本是 UCS4 并且要运行的版本是 UCS2 时,这会失败

关于python - 使用 PEX 将 python 脚本与 pandas 打包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33368713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com