1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[pip3] ImportErrorが発生する

Last updated at Posted at 2020-05-03

pip3 コマンドを実行するとエラーが発生する問題発生

ImportError: cannot import name 'pkg_resources' from 'pip._vendor'

原因

pkg_resourcespip._vendorパッケージにないとのこと

実際にpackageが存在しているかをCLIで確認したところ存在していることがわかった。
そして、ファイルの場所を確認したところpipで管理しているディレクトリに存在していないことが判明。

  • ファイルの存在確認
Python 3.7.7 (default, Mar 10 2020, 15:43:33) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pkg_resources
>>> pkg_resources.__file__
'/usr/local/lib/python3.7/site-packages/pkg_resources/__init__.py'
>>> 
  • pip起動時に必要なpackageのディレクトリ
    /Users/username/Library/Python/3.7/lib/python/site-packages/pip/_vendor/

解決策

  • パッケージファイルをpip管理下のディレクトリにコピー
  • パッケージファイルのシンボリックリンクをpip管理下のディレクトリに作成

今回はシンボリックリンクを作成する案で対応。
ln -s /usr/local/lib/python3.7/site-packages/pkg_resources /Users/username/Library/Python/3.7/lib/python/site-packages/pip/_vendor/pkg_resources

% pip3

Usage:   
  pip3 <command> [options]
.
.
.

めでたく実行できるようになりました。

参考(実際のエラーログ)

Traceback (most recent call last):
  File "/Library/Developer/CommandLineTools/usr/bin/pip3", line 10, in <module>
    sys.exit(main())
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/__init__.py", line 16, in main
    from pip._internal.utils.entrypoints import _wrapper
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/utils/entrypoints.py", line 3, in <module>
    from pip._internal.cli.main import main
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/cli/main.py", line 10, in <module>
    from pip._internal.cli.autocompletion import autocomplete
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/cli/autocompletion.py", line 9, in <module>
    from pip._internal.cli.main_parser import create_main_parser
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/cli/main_parser.py", line 7, in <module>
    from pip._internal.cli import cmdoptions
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/cli/cmdoptions.py", line 28, in <module>
    from pip._internal.models.target_python import TargetPython
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/models/target_python.py", line 4, in <module>
    from pip._internal.utils.misc import normalize_version_info
  File "/Users/username/Library/Python/3.7/lib/python/site-packages/pip/_internal/utils/misc.py", line 20, in <module>
    from pip._vendor import pkg_resources
ImportError: cannot import name 'pkg_resources' from 'pip._vendor' 
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?