pyinstaller command giving "ImportError: cannot import name 'Protocol' from 'typing' eror"

4,900 views
Skip to first unread message

Claes Sterregaard

unread,
Feb 14, 2022, 4:29:51 AM2/14/22
to PyInstaller
Hi All
I am trying to use pyinstaller to make an .exe of my dash app.
I first did pip install pyinstaller which went well.
But when I run the pyinstaller command I get the error below.
I then tried to make a basic project (app.basic.py) holding only a simple print('hallo') line to see if it was related to my dash app somehow but the error was raised again.
What is causing this and how can I solve it :-)? 

C:\users\claus\desktop\test>pyinstaller app_basic.py
Traceback (most recent call last):
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\importlib_metadata\_compat.py", line 9, in <module>
    from typing import Protocol
ImportError: cannot import name 'Protocol' from 'typing' (c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\typing.py)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\Claus\Anaconda3\pkgs\python-3.7.0-hea74fb7_0\Scripts\pyinstaller.exe\__main__.py", line 9, in <module>
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\PyInstaller\__main__.py", line 103, in run
    compat.check_requirements()
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\PyInstaller\compat.py", line 736, in check_requirements
    from importlib_metadata import distribution, PackageNotFoundError
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\importlib_metadata\__init__.py", line 17, in <module>
    from . import _adapters, _meta
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\importlib_metadata\_meta.py", line 1, in <module>
    from ._compat import Protocol
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\importlib_metadata\_compat.py", line 11, in <module>
    from typing_extensions import Protocol  # type: ignore
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\typing_extensions.py", line 494, in <module>
    OrderedDict = typing._alias(collections.OrderedDict, (KT, VT))
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\typing.py", line 1191, in _alias
    return _GenericAlias(origin, params, special=True, inst=inst)
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\typing.py", line 613, in __init__
    self.__parameters__ = _collect_type_vars(params)
  File "c:\users\claus\anaconda3\pkgs\python-3.7.0-hea74fb7_0\lib\site-packages\typing_extensions.py", line 143, in _collect_type_vars
    not isinstance(t, _UnpackAlias)
NameError: name '_UnpackAlias' is not defined

Steve Barnes

unread,
Feb 15, 2022, 1:05:52 AM2/15/22
to pyins...@googlegroups.com

This is an easy one - your code is trying to:

  1. Use typing.Protocol which was introduced in Python 3.8 https://docs.python.org/3/library/typing.html#typing.Protocol
  2. Build and executable using PyInstaller using Python 3.7 from the error message paths below.

 

To resolve this I would suggest:

  1. create a venv or equivalent that specifically uses python 3.8 or greater,
  2. activate it and
  3. install your dependencies including PyInstaller
  4. Build your executable

Then going forwards make sure that you use steps 2 & 4 every time that you build your code.

 

Hope that helps

 

Steve Barnes

--
You received this message because you are subscribed to the Google Groups "PyInstaller" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/47695ca5-37ac-4eb7-b93e-2ff89f6170d2n%40googlegroups.com.

Claes Sterregaard

unread,
Feb 15, 2022, 3:01:27 AM2/15/22
to pyins...@googlegroups.com
Dear Steve.

Thank you for your kind reply.
I still wonder though - my app_basic.py does not include anything, it is just a print statement.
I can run the app_basic.py (as well as the actual program I eventually want to make an .exe for) without issues in my 3.7 environment.
Why is "typing.Protocol" suddenly in play when I run the pyinstaller command then?

All the best
Claes

You received this message because you are subscribed to a topic in the Google Groups "PyInstaller" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyinstaller/K6liK5_GqV4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyinstaller...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyinstaller/VI1PR03MB657526F5BD270019C42003649B349%40VI1PR03MB6575.eurprd03.prod.outlook.com.

Steve Barnes

unread,
Feb 15, 2022, 5:54:17 AM2/15/22
to pyins...@googlegroups.com

Hi Claes,

 

It looks from your error messages as if the Anaconda installation of Python 3.7 has had the importlib-metadata library installed which is introducing this error. I would suggest, if you can, upgrading to at least Python 3.9 (3.10 is the current python) and using that as Python 3.7 ends security support in 1 year & 4 months. If you have to support Windows 7 then you would have to go for Python 3.8.

 

If you can't then you will need to either uninstall or downgrade importlib-metadata - if it was pip installed then pip uninstall importlib-metadata should do the job but as I rarely use Anaconda I am not sure how to do it with conda. Of course if you need to use importlib-metadata, (e.g. for keyring or twine) then you may be stuck with needing to upgrade your python.

 

Hope that is some help!

 

Steve

 

From: pyins...@googlegroups.com <pyins...@googlegroups.com> On Behalf Of Claes Sterregaard
Sent: 15 February 2022 07:48
To: pyins...@googlegroups.com
Subject: Re: [PyInstaller] pyinstaller command giving "ImportError: cannot import name 'Protocol' from 'typing' eror"

 

Dear Steve.

 

Thank you for your kind reply.

I still wonder though - my app_basic.py does not include anything, it is just a print statement.

I can run the app_basic.py (as well as the actual program I eventually want to make an .exe for) without issues in my 3.7 environment.

Why is "typing.Protocol" suddenly in play when I run the pyinstaller command then?

 

All the best

Claes

 

Den tir. 15. feb. 2022 kl. 07.05 skrev Steve Barnes <Gadge...@live.co.uk>:

This is an easy one - your code is trying to:

  1. Use typing.Protocol which was introduced in Python 3.8 https://docs.python.org/3/library/typing.html#typing.Protocol
  2. Build and executable using PyInstaller using Python 3.7 from the error message paths below.

 

To resolve this I would suggest:

1.       create a venv or equivalent that specifically uses python 3.8 or greater,

2.       activate it and

3.       install your dependencies including PyInstaller

4.       Build your executable

Reply all
Reply to author
Forward
0 new messages