0

I am working on a news crawling program and want to take my python file and turn it into an executable application. But I am having a lot of trouble with the newspaper3k library. My program works fine on PyCharm, but when I try to run the executable functions relying on newspaper3k are broken. I get the following error:

Error occurred while processing author and published date for https://www.npr.org/2023/05/04/1174015808/nfl-investigation-claims-gender-discrimination-harassment#:~:text=The%20attorneys%20general%20of%20California: [Errno 2] No such file or directory: '/Users/.../dist/headline_hunter/newspaper/resources/text'

I tried a number of different things, including creating a hook file and a spec file and using them with pyinstaller:

from PyInstaller.utils.hooks import collect_data_files

datas = collect_data_files('newspaper3k')

and

# -*- mode: python ; coding: utf-8 -*-

from PyInstaller.utils.hooks import collect_data_files

block_cipher = None

datas = collect_data_files('newspaper3k')

a = Analysis(
    ['headline_hunter.py'],
    pathex=[],
    binaries=[],
    datas=datas,
    hiddenimports=[],
    hookspath=['.'],
    hooksconfig={},
    runtime_hooks=[],
    excludes=[],
    win_no_prefer_redirects=False,
    win_private_assemblies=False,
    cipher=block_cipher,
    noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(
    pyz,
    a.scripts,
    a.binaries,
    a.zipfiles,
    a.datas,
    [],
    name='headline_hunter',
    debug=False,
    bootloader_ignore_signals=False,
    strip=False,
    upx=True,
    upx_exclude=[],
    runtime_tmpdir=None,
    console=True,
    disable_windowed_traceback=False,
    argv_emulation=False,
    target_arch=None,
    codesign_identity=None,
    entitlements_file=None,
)

I also tried copying the resources folder into the same directory as my script and doing this: pyinstaller --add-data "resources:./newspaper/resources" headline_hunter.py Any help would be much appreciated.

1 Answers1

0

You could try this, and if it doesn't work try the other:

  1. Use the --hidden-import option to explicitly tell PyInstaller to include newspaper3k and its dependencies in the executable. For example, you could run:
pyinstaller --hidden-import newspaper --hidden-import newspaper.article --hidden-import newspaper.configuration --hidden-import newspaper.extractors --hidden-import newspaper.network --hidden-import newspaper.news_pool --hidden-import newspaper.scraper --hidden-import newspaper.settings --hidden-import newspaper.source --hidden-import newspaper.utils headline_hunter.py

This will tell PyInstaller to include all the modules that the newspaper3k library depends on.

  1. Use the --add-data option to add the newspaper3k resources to the executable. For example, you could run:
pyinstaller --add-data '/path/to/newspaper/resources;newspaper/resources' headline_hunter.py
  1. If that still doesn't work, just try using the --onefile ability:
pyinstaller --onefile headline_hunter.py