crs4 / hl7apy

Python library to parse, create and handle HL7 v2 messages.
http://crs4.github.io/hl7apy/
MIT License
224 stars 88 forks source link

hl7apy import - Getting a missing file error after creating the exe file and running the exe file #87

Closed bnaoe closed 3 years ago

bnaoe commented 3 years ago

I'm importing an hl7apy module to the script it looks like it is looking for some files but not sure how to get all those. I'm attaching the code I have, it's in .ipynb but converted to .py using ipython nbconvert --to script .\Covid.ipynb then ran the command pyinstaller --debug-all .\Covid.py. The executable file gets generated however when I run the exe file it throws the error below.

Please advise. Thank you

ipynb code

import numpy as np
import pandas as pd
from hl7apy.core import Message
from hl7apy.parser import parse_message
from tkinter import Tk
from tkinter.filedialog import askopenfilename

Tk().withdraw()
filename = askopenfilename()

df = pd.read_csv(filename)

df['RACE'] = df[['RACE_AMERICAN_INDIAN_AK_NATIVE','RACE_ASIAN','RACE_BLACK_AFRICAN_AMERICAN'
                 ,'RACE_NTV_HAWAIIAN_PACIFIC_ISL','RACE_WHITE','RACE_OTHER']].apply(lambda x: "".join(x.dropna()), axis=1)

df.drop(columns=['RACE_AMERICAN_INDIAN_AK_NATIVE','RACE_ASIAN','RACE_BLACK_AFRICAN_AMERICAN'
                 ,'RACE_NTV_HAWAIIAN_PACIFIC_ISL','RACE_WHITE','RACE_OTHER'], inplace=True)

df['ADMINISTERED_TIME'] = df['ADMINISTERED_TIME'].fillna('00:00')

df_covid = df.copy()

m = Message("VXU_V04")

from datetime import datetime

today = datetime.today()
dt_msg = today.strftime("%Y%m%d%H%M%S")

for index, row in df_covid.iterrows():

    #MSH
    m.msh.msh_3 = 'IS'
    m.msh.msh_4 = '77777'
    m.msh.msh_5 = 'Receiving Application'
    m.msh.msh_7 = str(dt_msg)
    m.msh.msh_9 = 'VXU^V04^VXU_V04'

    #PID
    m.pid.pid_1 = '1'
    m.pid.pid_3 = str(df_covid.iloc[index]['PARTICIPANT_ID']) + '^^^^MRN^'
    m.pid.pid_5 = str(df_covid.iloc[index]['LAST_NAME']) + '^' + str(df_covid.iloc[index]['FIRST_NAME'])            
    m.pid.pid_7 = datetime.strptime(df_covid.iloc[index]['BIRTH_DATE'],'%m/%d/%Y 0:00').strftime("%Y%m%d")
    m.pid.pid_8 = str(df_covid.iloc[index]['GENDER'])
    m.pid.pid_10 = str(df_covid.iloc[index]['RACE'])
    m.pid.pid_11 = str(df_covid.iloc[index]['ADDRESS1']) + '^' + str(df_covid.iloc[index]['ADDRESS2']) +'^' + str(df_covid.iloc[index]['CITY']) + '^' + str(df_covid.iloc[index]['STATE']) + '^' + str(df_covid.iloc[index]['ZIP']) + '^USA'                                                 

    #ORC
    m.orc.orc_1 = 'RE'
    m.orc.orc_9 = str(dt_msg)
    m.orc.orc_10 = '^' + str(df_covid.iloc[index]['ADMINISTERED_BY'])
    m.orc.orc_11 = 'RXA'
    m.orc.orc_12 = '0'
    m.orc.orc_13 = '1'

    #OBX
    m.obx.obx_3 = '64994-7'

    #RXA
    m.rxa.rxa_1 = '0'
    m.rxa.rxa_2 = '1'
    m.rxa.rxa_3 = datetime.strptime(str(df_covid.iloc[index]['DOSE_ADMINISTERED_DATE']) + ' '
                                    + str(df_covid.iloc[index]['ADMINISTERED_TIME']),"%m/%d/%Y %H:%M").strftime("%Y%m%d%H%M%S")
    m.rxa.rxa_4 = datetime.strptime(str(df_covid.iloc[index]['DOSE_ADMINISTERED_DATE']) + ' '
                                    + str(df_covid.iloc[index]['ADMINISTERED_TIME']),"%m/%d/%Y %H:%M").strftime("%Y%m%d%H%M%S")
    m.rxa.rxa_5 = str(df_covid.iloc[index]['CVX_CODE'])
    m.rxa.rxa_9 = '00'
    m.rxa.rxa_11 = '77777'
    m.rxa.rxa_15 = str(df_covid.iloc[index]['LOT'])
    m.rxa.rxa_17 = str(df_covid.iloc[index]['MANUFACTURER'])
    m.rxa.rxa_21 = 'A'

    df_covid.loc[index,'Hl7'] = m.to_er7().replace('\r','\n')

    with open(str(index) + '_some_file.txt', 'w') as f:
        f.write(m.to_er7().replace('\r','\n'))
        f.close()

command line error after running the exe file

File "C:\Users\user\DataNow\COVID-19\Covid.py", line 9, in <module>
    from hl7apy.core import Message
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 975, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 671, in _load_unlocked
  File "c:\users\user\anaconda3\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "hl7apy\__init__.py", line 332, in <module>
  File "hl7apy\__init__.py", line 329, in _discover_libraries
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\user\\AppData\\Local\\Temp\\_MEI209722\\hl7apy'
[28568] Failed to execute script Covid
bnaoe commented 3 years ago

Never mind, resolved this by I copying all the missing files to the dist folder. Thanks.

kwiechen commented 3 years ago

which files were missing?

Best regards,

Kai

cschwerdtner commented 1 year ago

I've had the same problem. Solution is this: copy folder hl7apy to dist/main folder. Regards, Chris

HenryGarcia96 commented 4 months ago

Hi, I have the same doubt, pyinstaller ignores the hl7apy folder, but my solution is , if you build the executable via directory the manual solution is to copy hl7apy to the dist/main folder, not via executable.

Now for it to compile properly you have to pass flags to pyinstaller so that it tries to compile hl7apy (in my case an executable).

pyinstaller main.py -F -n="Application" -i="shadow.ico" --log-level DEBUG > out.txt --additional-hooks-dir=hooks-basicsr.py --paths=C:Users\USER\PycharmProjects\pythonProject\venvsite-packages --hidden-import hl7apy --collect-all hl7apy --clean

This was my first successful attempt to import the library. But my current hypothesis is that only the last thing I added to the command actually worked leaving the pyinstaller command with the following configuration:

pyinstaller main.py -n="Application" -i="shadow.ico" --collect-all hl7apy --clean

Indeed only --collect-all allows the import of the library.

Regards to all