Python script package with pandas using pex

I have a simple python script that depends on pandas. I need to pack it with pex so that it runs without installing dependencies.

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()

I am trying to use pex for this, so I did the following:

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

But I get this error when running the generated pex file:

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

I also tried packing with -c (switch for script) using the following command:

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

But also getting the error:

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
+4
source share
1 answer

The only option that has worked for me so far is to create a pex interpreter that includes pandas and then send it using a python script. This can be done as follows:

pex pandas -o my_interpreter.pex

, python - UCS4, - UCS2

0

Source: https://habr.com/ru/post/1613346/


All Articles