简体   繁体   中英

Import Error building an executable with py2exe

I program a software that connects with a mqtt broker and displays windows10 notifications using win10toaster among others. I wanted to create an executable with py2exe but when I run the .exe I get this output:


Traceback (most recent call last):
  File "suscribe.py", line 7, in <module>
  File "zipextimporter.pyc", line 168, in exec_module
  File "win10toast\__init__.pyc", line 15, in <module>
  File "zipextimporter.pyc", line 168, in exec_module
  File "pkg_resources\__init__.pyc", line 75, in <module>
  File "pkg_resources\extern\__init__.pyc", line 52, in create_module
  File "pkg_resources\extern\__init__.pyc", line 44, in load_module
ImportError: The 'packaging' package is required; normally this is bundled with this package so if you get this warning, consult the packager of your distribution.

my setup.py is the following:

from distutils.core import setup
import py2exe
from playsound import playsound 
import random
import time

from paho.mqtt import client as mqtt_client
from win10toast import ToastNotifier
packages = ['pkg_resources','win10toast','zipextimporter']
setup(
    name="Nombre ejecutable",
    version="1.0",
    description="Breve descripcion",
    author="autor",
    author_email="email del autor",
    url="url del proyecto",
    license="tipo de licencia",
    scripts=["suscribe.py"],
    console=["suscribe.py"],
    options={"py2exe": {"bundle_files": 1},
            'build_exe': {'packages':packages}
    },
    zipfile=None,
)
And this is my imports in the main .py:
from playsound import playsound 
import random
import time

from paho.mqtt import client as mqtt_client
from win10toast import ToastNotifier

I tried several things but I still get that Import - Error.

any ideas?

thanks in advance!

The package 'pkg_resources' is loading the package 'packaging' dinamically instead of a Import statement. You can check this from the last line at the github repository https://github.com/pypa/setuptools/blob/main/pkg_resources/extern/__init__.py In fact, they are installing the package if it is not included, but in the case of py2exe environment, it is not possible, therefore, an error is raised.

You can solve this by putting 'packaging' in your setup.py packages list Maybe you will need to repeat the step if any more package is missing

i can solve this problem.

The solution came after that i include the following in my code:

import packaging
import packaging.version
import packaging.specifiers
import packaging.requirements

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM