2016-08-10 6 views
2

파이썬 스크립트 용 실행 파일을 만들기 위해 PyInstaller를 설치했는데 제대로 작동합니다. 필자는 PyPandoc을 사용하여 .docx 보고서를 만들었습니다. 일반 Python 파일이 실행될 때 정상적으로 실행되지만 PyInstaller 실행 파일에서는 실행되지 않습니다.PyPandoc with PyInstaller

Traceback (most recent call last): 
    File "src\flexmodel_postcalc.py", line 295, in postcalculate_everything 
    File "src\flexmodel_postcalc.py", line 281, in generate_report_docx 
    File "src\flexmodel_report_docx.py", line 118, in generate_text_useages_docx 
    File "pypandoc\__init__.py", line 50, in convert 
    File "pypandoc\__init__.py", line 70, in _convert 
    File "pypandoc\__init__.py", line 197, in get_pandoc_formats 
    File "pypandoc\__init__.py", line 336, in _ensure_pandoc_path 
OSError: No pandoc was found: either install pandoc and add it 
to your PATH or install pypandoc wheels with included pandoc. 

실행 파일을 만드는 동안 PyPandoc에 관한 이상한 문제는 없습니다. Python 및/또는 Pandoc 설치가없는 다른 사람들이 실행 파일을 사용하고 .docx 보고서를 만들 수 있도록 Pandoc을 실행 파일에 포함하려면 어떻게해야합니까?

편집 : pythonfile.py

  • 실행 파일을 만들 때

    import pypandoc 
    pypandoc.convert(sou‌​rce='# Sample title\nPlaceholder', to='docx', format='md', outputfile='test.doc‌​x') 
    
  • 이 파일을 저장 :

    1. 다음 코드를 포함한 파일을 만듭니다 작업 프로세스는 다음 단계를 포함 PyInstaller :

      pyinstaller --onefile --clean pythonfile.py 
      
    2. 이제 실행 파일은 Pandoc (또는 PyPandoc)이 설치되어 있지 않은 컴퓨터에서 실행해야합니다.

  • +1

    파이썬 스크립트의 최소 작동 예제를 추가하여 사용중인 pandoc의 부분을 확인할 수 있습니까? 나는'pypandoc' 만 시도해 봤고 내 Windows 시스템에서 정상적으로 작동하는 것 같습니다. 대부분 'pypandoc'을 [숨겨진 가져 오기] (https://pythonhosted.org/PyInstaller/when-things-go-wrong.html#listing-hidden-imports)와 다른 의존성에 추가해야합니다. – Repiklis

    +0

    @Repiklis 아주 짧은 샘플 코드를 제공 할 수는 있지만 지금은 테스트하지 마십시오. 'import pypandoc; pypandoc.convert (source = '# 샘플 제목 \ nPlaceholder', to = 'docx', format = 'md', outputfile = 'test.docx')'. 이것은'.py' 파일에 저장됩니다.이 파일은'pyinstaller --onefile --clean -p H : \ AppData \ Python \ Python27 \ Lib \ site-packages pythonfile.py' 명령으로 변환합니다. – Mathias711

    +0

    게시하십시오 다른 사용자가 귀하의 문제를 이해할 수 있도록 질문의 샘플 코드. 아래에 답변을 게시하겠습니다. – Repiklis

    답변

    3

    여기에는 두 가지 문제가 있습니다. 첫 번째는 pypandocpandoc.exe이 필요하다는 것입니다. 이것은 pyinstaller에 의해 자동으로 선택되지 않지만 수동으로 지정할 수 있습니다.

    이렇게하려면 create a .spec file해야합니다. 이처럼 내가 생성 한 및 사용 외모 :

    block_cipher = None 
    
    a = Analysis(['pythonfile.py'], 
          pathex=['CodeDIR'], 
          binaries=[], 
          datas=[], 
          hiddenimports=[], 
          hookspath=[], 
          runtime_hooks=[], 
          excludes=[], 
          win_no_prefer_redirects=False, 
          win_private_assemblies=False, 
          cipher=block_cipher) 
    pyz = PYZ(a.pure, a.zipped_data, 
          cipher=block_cipher) 
    exe = EXE(pyz, 
          a.scripts, 
          a.binaries, 
          a.zipfiles, 
          a.datas, 
          name='EXEName', 
          debug=False, 
          strip=False, 
          upx=True, 
          console=True , 
          resources=['YourPandocLocationHere\\\\pandoc.exe']) 
    

    당신은 pyinstaller myspec.spec를 사용하여 실행 파일을 구축 할 수 있습니다. 경로와 name 매개 변수를 변경하는 것을 잊지 마십시오.

    디렉토리 모드에서 빌드하는 경우이 정도면 충분합니다. 그러나 one-file 모드의 경우 pyinstaller bootloader process이 작동하는 방식 때문에 작업이 좀 더 복잡해집니다. pandoc.exe 파일은 임시 폴더에서 실행 중에 압축이 풀리지 만 원래 .exe 폴더에서 실행됩니다. this question에 따르면 고정 된 코드를 실행하는 경우 현재 폴더를 변경하기 위해 pypandoc을 호출하기 전에 코드에 다음 줄을 추가해야합니다.

    if hasattr(sys, '_MEIPASS'): 
        os.chdir(sys._MEIPASS) 
    
    +0

    게시 해 주셔서 감사합니다.어떤 이유로 든'-p' 인수가 작동하지 않지만 최소한 올바른 길을 가고 있습니다. 감사! – Mathias711

    +0

    다음과 같이 컴파일하려면'.spec' 파일 끝에 ['collect' 블록] (https://pythonhosted.org/PyInstaller/spec-files.html#spec-file-operation)을 추가해야합니다. '하나의 dir'. – Repiklis

    +0

    'resources' 인자가리스트가 아니어야한다는 것이 맞습니까? 내가 TypeError를 가지고 있고,'[]'가 없으면 작동합니다. 나중에 참조 할 수 있도록'-p' 인수 폴더를'pathex' 인수 (이 목록의 새 항목)에 포함 시켰습니다. – Mathias711