1

Like this question:How to input arguments after compiling python program with PyInstaller For personal reasons,I need to use scala to start a python HTTP server, and the port needs to be randomly generated and pass to the python program. I need to compile the python program to an executable file first, with all dependencies, so I decided to use pyinstaller.

from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import sys
import numpy as np
from sklearn import neighbors

host = ('localhost', int(sys.argv[1])) # the port is passed here
slide_window = []

class Resquest(BaseHTTPRequestHandler):
    def do_POST(self):
        content_len = int(self.headers['Content-Length'])   
        post_body = self.rfile.read(content_len)
        update_data = json.loads(post_body)
        slide_window.append(update_data)
        if len(slide_window) >= 100:
            result = regression()
            self.send_response(200)
            self.send_header('Content-type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(result).encode())
            del slide_window[:11]
        else:
            self.send_response(200)
            empty_data = []
            self.send_header('Content-type', 'application/json')
            self.end_headers()
            self.wfile.write(json.dumps(empty_data).encode())
        
    
    
def regression():
 
    timestamp = np.arange(0,10,0.1).reshape(100,1)
    knn = neighbors.KNeighborsRegressor(5, weights="distance")
    knn.fit(timestamp, slide_window)
    predict_timestamp = np.array(10,11,0.1).reshape(10,1)
    result_frequency = knn.predict(predict_timestamp)
    return result_frequency


if __name__ == '__main__':
    server = HTTPServer(host, Resquest)
    server.serve_forever()

When I use like

python3 regressionServer.py 9999

It works fine. But after I used

pyinstaller -F regressionServer.py

it created an executable file in dist directory. I couldn't exec dist/regressionServer 9999. It told me this error:

> ValueError: invalid literal for int() with base 10: '-B'

So I printed sys.argv and found that

['/root/github.com/jessestutler/regressionServer/dist/regressionServer', '-B', '-S', '-E', '-s', '-c', 'from multiprocessing.semaphore_tracker import main;main(3)']

The problem is that args are fixed and I can't pass my arguments to the executable file now. How to solve it?

Jesse Stutler
  • 147
  • 2
  • 7
  • what is the value of `sys.argv` if you print it out inside `if __name__ == "__main__"` block? – Krerkkiat Chusap Apr 18 '22 at 05:37
  • It's the same. Just `['/root/github.com/jessestutler/regressionServer/dist/regressionServer', '-B', '-S', '-E', '-s', '-c', 'from multiprocessing.semaphore_tracker import main;main(3)']` – Jesse Stutler Apr 19 '22 at 01:50
  • I cannot reproduce the problem even with your source code on Python 3.10.2, Pyinstaller 4.9. The command line arguments are passed to the program just fine. – Krerkkiat Chusap Apr 20 '22 at 23:27
  • Weird...I also think it's supposed to work. – Jesse Stutler Apr 22 '22 at 07:09
  • are you on an older version of python? `semaphore_tracker` seems to be on 3.6, but with newer version, it is `resource_tracker`. It also kind of imply that the code is using `multiprocessing` module, but that is not in the code you provided (unless `HTTPServer` is using it and I am not aware). – Krerkkiat Chusap Apr 22 '22 at 16:07
  • one more different tho, I leave out the `sklearn` from the code I used to test (couldn't be brother to install it ). I can try adding that back it and see if it make any different. – Krerkkiat Chusap Apr 22 '22 at 16:10
  • Yes, you are right. After I deleted sklearn related code, the pyinstaller created file can be executed successfully by passing arguments... But I need sklearn related features.. – Jesse Stutler Apr 24 '22 at 02:57

0 Answers0