AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'? Occurs When Running pipenv install → First, Check the Python Version of pipenv
To set up a Python 3.12 environment, I encountered the following error when creating a new virtual environment with Pipenv and running pipenv install:
Traceback (most recent call last):
File "<my-env>/.venv/bin/pip", line 5, in <module>
from pip._internal.cli.main import main
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/main.py", line 9, in <module>
from pip._internal.cli.autocompletion import autocomplete
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/autocompletion.py", line 10, in <module>
from pip._internal.cli.main_parser import create_main_parser
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/main_parser.py", line 8, in <module>
from pip._internal.cli import cmdoptions
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/cmdoptions.py", line 23, in <module>
from pip._internal.cli.parser import ConfigOptionParser
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/parser.py", line 12, in <module>
from pip._internal.configuration import Configuration, ConfigurationError
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/configuration.py", line 20, in <module>
from pip._internal.exceptions import (
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/exceptions.py", line 7, in <module>
from pip._vendor.pkg_resources import Distribution
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2164, in <module>
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
In this case, running
% python3 -m ensurepip --upgrade
is likely to solve the issue. It is advisable to do this both in the system global python environment and within the virtual environment.
However, since the Pipenv environment was problematic, I decided to recreate the Pipenv environment from scratch.
Checking the System Environment
Checking the Default Version of Python3 (Mac)
Without entering the virtual environment (venv):
% which python3
/opt/homebrew/bin/python3
% python3 --version
Python 3.11.6
It shows that /opt/homebrew/bin/python3 is being used and it's Python 3.11.
Checking the Version of Python Used by Pipenv
Without entering the virtual environment (venv):
% which pipenv
/opt/homebrew/bin/pipenv
% head /opt/homebrew/bin/pipenv
#!/opt/homebrew/opt/python@3.9/bin/python3.9
# -*- coding: utf-8 -*-
import re
import sys
from pipenv import cli
...
The Pipenv is set to use Python 3.9.
Since the version of Python used by Pipenv and the default Python version are different, this could cause complications. Therefore, I decided to configure Pipenv to use the default Python 3.11.
Reinstalling System Global Pipenv
Uninstalling via Homebrew
% brew uninstall pipenv
Error: No such keg: /opt/homebrew/Cellar/pipenv
It was not installed via Homebrew.
Deleting the File
It's likely installed via Python 3.9's pip.
In hindsight, running
% /opt/homebrew/opt/python@3.9/bin/python3.9 -m pip uninstall pipenv
might have cleanly uninstalled it. However, since I intended to recreate it in the Python 3.11 environment, I just deleted it manually.
% rm /opt/homebrew/bin/pipenv
Reinstalling Pipenv Using the Default Python3 in the System Global Environment
% python3 -m pip install pipenv
Verifying the Installation Result
% which pipenv
/opt/homebrew/bin/pipenv
The file that was deleted has been regenerated as expected.
% head /opt/homebrew/bin/pipenv
#!/opt/homebrew/opt/python@3.11/bin/python3.11
# -*- coding: utf-8 -*-
import re
import sys
from pipenv import cli
...
It is now using Python 3.11, as expected.
For Good Measure, Running Ensurepip Upgrade in the System Global Python Environment
% python3 -m ensurepip --upgrade
Creating the Virtual Environment
Notes on creating a Pipenv environment with Python 3.12:
Pipfile
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true
[dev-packages]
[packages]
[requires]
python_version = "3.12"
After creating this file, run
% python3 -m pipenv install
As long as the first line (shebang) of head $(which pipenv) matches the path of the python shown by which python3, you can also run
% pipenv install
Entering the Virtual Environment
% python3 -m pipenv shell
Testing pip Within the Virtual Environment
Inside the virtual environment, run:
python3 -m pip freeze
or
pip freeze
to check for any errors.
If the error
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/main_parser.py", line 8, in <module>
from pip._internal.cli import cmdoptions
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/cmdoptions.py", line 23, in <module>
from pip._internal.cli.parser import ConfigOptionParser
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/cli/parser.py", line 12, in <module>
from pip._internal.configuration import Configuration, ConfigurationError
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/configuration.py", line 20, in <module>
from pip._internal.exceptions import (
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_internal/exceptions.py", line 7, in <module>
from pip._vendor.pkg_resources import Distribution
File "<my-env>/.venv/lib/python3.12/site-packages/pip/_vendor/pkg_resources/__init__.py", line 2164, in <module>
register_finder(pkgutil.ImpImporter, find_on_path)
^^^^^^^^^^^^^^^^^^^
AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
appears, try running
% python3 -m ensurepip --upgrade
while inside the virtual environment.
We look forward to discussing your development needs.