PyInstaller:解决“TypeError: expected str, bytes or os.PathLike object, not NoneType”问题

当使用Python 3.7.2和PyInstaller 3.4时,会出现“TypeError: expected str, bytes or os.PathLike object, not NoneType”错误,解决办法是直接修改venv/Lib/site-packages/PyInstaller/depend/bindepend.py文件,添加

if is_win and 'VERSION.dll' in dlls:
        pydll = 'python%d%d.dll' % sys.version_info[:2]
        if pydll in PYDYLIB_NAMES:
            filename = getfullnameof(pydll)
            return filename

到函数get_python_library_path里面,修改后的内容如下所示:

def get_python_library_path():
    """
    Find dynamic Python library that will be bundled with frozen executable.

    NOTOE: This is a fallback option when Python library is probably linked
    statically with the Python executable and we need to search more for it.
    On Debian/Ubuntu this is the case.

    Return  full path to Python dynamic library or None when not found.


    We need to know name of the Python dynamic library for the bootloader.
    Bootloader has to know what library to load and not trying to guess.

    Some linux distributions (e.g. debian-based) statically build the
    Python executable to the libpython, so bindepend doesn't include
    it in its output. In this situation let's try to find it.

    Darwin custom builds could possibly also have non-framework style libraries,
    so this method also checks for that variant as well.
    """
    # Try to get Python library name from the Python executable. It assumes that Python
    # library is not statically linked.
    dlls = getImports(sys.executable)
    for filename in dlls:
        for name in PYDYLIB_NAMES:
            if os.path.basename(filename) == name:
                # On Windows filename is just like 'python27.dll'. Convert it
                # to absolute path.
                if is_win and not os.path.isabs(filename):
                    filename = getfullnameof(filename)
                # Python library found. Return absolute path to it.
                return filename

    # Python library NOT found. Resume searching using alternative methods.
    # Applies only to non Windows platforms.
    if is_win and 'VERSION.dll' in dlls:
        pydll = 'python%d%d.dll' % sys.version_info[:2]
        if pydll in PYDYLIB_NAMES:
            filename = getfullnameof(pydll)
            return filename

    if is_unix:
        for name in PYDYLIB_NAMES:
            python_libname = findLibrary(name)
            if python_libname:
                return python_libname

    elif is_darwin:
        # On MacPython, Analysis.assemble is able to find the libpython with
        # no additional help, asking for sys.executable dependencies.
        # However, this fails on system python, because the shared library
        # is not listed as a dependency of the binary (most probably it's
        # opened at runtime using some dlopen trickery).
        # This happens on Mac OS X when Python is compiled as Framework.

        # Python compiled as Framework contains same values in sys.prefix
        # and exec_prefix. That's why we can use just sys.prefix.
        # In virtualenv PyInstaller is not able to find Python library.
        # We need special care for this case.
        # Anaconda places the python library in the lib directory, so
        # we search this one as well.
        prefixes = [compat.base_prefix, os.path.join(compat.base_prefix, 'lib')]
        for prefix in prefixes:
            for name in PYDYLIB_NAMES:
                full_path = os.path.join(prefix, name)
                if os.path.exists(full_path):
                    return full_path

    # Python library NOT found. Return just None.
    return None

参考链接

  1. https://github.com/Loran425/pyinstaller/commit/14b6e65642e4b07a4358bab278019a48dedf7460
  2. https://stackoverflow.com/questions/54138898/an-error-for-generating-an-exe-file-using-pyinstaller-typeerror-expected-str

你可能感兴趣的:(Python)