Pythonで「インポートエラー」を解決するには?

Pythonで「インポートエラー」を解決するには?

必須モジュールがインストールされましたが、インポート中にlxmlエラーが発生しました。ImportError: No module named lxml

私はどのように知っています

  1. Pythonモジュールはどこで考慮する必要がありますか?
  2. どのPythonバージョンとpipを使用または設定していますか?

この問題を理解し解決するのに役立ちます。

uname -a
Linux machine-name 4.4.0-31-generic #50~14.04.1-Ubuntu SMP Wed Jul 13 01:07:32 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

which python
/usr/local/bin/python

which pip
/usr/local/bin/pip

pip --version
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
  utils.PersistentlyDeprecated2018,
pip 18.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)

python
Python 2.7.16 (default, May  6 2020, 13:05:58) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> import urllib2
>>> os.path.abspath(urllib2.__file__)
'/usr/local/lib/python2.7/urllib2.pyc'

echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

echo $PYTHONPATH
doesn't display anything (empty)


ls -ls /usr/bin/python*

   0 lrwxrwxrwx 1 root root       9  /usr/bin/python -> python2.7
   0 lrwxrwxrwx 1 root root      16  /usr/bin/python-config -> python2.7-config
   0 lrwxrwxrwx 1 root root       9  /usr/bin/python2 -> python2.7
   0 lrwxrwxrwx 1 root root      16  /usr/bin/python2-config -> python2.7-config
3264 -rwxr-xr-x 1 root root 3341288  /usr/bin/python2.7
   0 lrwxrwxrwx 1 root root      33  /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
   0 lrwxrwxrwx 1 root root       9  /usr/bin/python3 -> python3.4
3628 -rwxr-xr-x 2 root root 3714088  /usr/bin/python3.4
3628 -rwxr-xr-x 2 root root 3714088  /usr/bin/python3.4m
   0 lrwxrwxrwx 1 root root      10  /usr/bin/python3m -> python3.4m
   
ls -ls /usr/local/bin/python*
   0 lrwxrwxrwx 1 root root       9  /usr/local/bin/python -> python2.7
   0 lrwxrwxrwx 1 root root      14  /usr/local/bin/python-config -> python2-config
   0 lrwxrwxrwx 1 root root       7  /usr/local/bin/python.orig -> python2
   0 lrwxrwxrwx 1 root root       9  /usr/local/bin/python2 -> python2.7
   0 lrwxrwxrwx 1 root root      16  /usr/local/bin/python2-config -> python2.7-config
8280 -rwxr-xr-x 1 root root 8478000  /usr/local/bin/python2.7
   4 -rwxr-xr-x 1 root root    1687  /usr/local/bin/python2.7-config

find /bin /usr /lib* /home -type f -name "pip*" -executable
/usr/local/bin/pip
/usr/local/bin/pip2.7
/usr/local/bin/pip2
/usr/bin/pip
/usr/bin/pip2

/usr/bin/pip install lxml
Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/dist-packages
Cleaning up...

/usr/bin/pip2 install lxml
Requirement already satisfied (use --upgrade to upgrade): lxml in /usr/local/lib/python2.7/dist-packages
Cleaning up...

/usr/local/bin/pip2 install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
  utils.PersistentlyDeprecated2018,
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)

/usr/local/bin/pip2.7 install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
  utils.PersistentlyDeprecated2018,
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)

/usr/local/bin/pip install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
  utils.PersistentlyDeprecated2018,
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)

sudo pip install lxml
/usr/local/lib/python2.7/dist-packages/cryptography/hazmat/primitives/constant_time.py:26: CryptographyDeprecationWarning: Support for your Python version is deprecated. The next version of cryptography will remove support. Please upgrade to a release (2.7.7+) that supports hmac.compare_digest as soon as possible.
  utils.PersistentlyDeprecated2018,
The directory '/home/abcd/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/abcd/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Requirement already satisfied: lxml in /usr/local/lib/python2.7/dist-packages (4.6.3)

python -m pip install lxml
/usr/local/bin/python: No module named pip

python
Python 2.7.16 (default, May  6 2020, 13:05:58) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import lxml
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named lxml

import sys
print("\n".join(sys.path))

/usr/local/lib/python27.zip
/usr/local/lib/python2.7
/usr/local/lib/python2.7/plat-linux2
/usr/local/lib/python2.7/lib-tk
/usr/local/lib/python2.7/lib-old
/usr/local/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/site-packages

ベストアンサー1

Pythonがシステム全体にインストールされているパッケージを見つけることを許可するには:

/usr/local/lib/python2.7/site-packagesディレクトリからdistpackages.pth(または.pth拡張子を持つファイル;ハイフンを避けながら明確な名前を選択しました)というファイルを作成します。

このファイルから次の行を作成します。

../dist-packages

それはすべてです。この行は、コンテンツがインストールされる/usr/local/lib/python2.7/dist-packagesディレクトリを設定することを意味しますpip。 Pythonはそのファイルを読み込み、その中に行を追加し、インストールされたsys.pathパッケージを見つける場所を見つけます。

これの説明は にあります。Pythonサイトのドキュメント。この答えは本質的にこの答えはスーパーユーザーに関するものです。


pip通常とは異なる場所に物を設置するように構成する理由と方法がわかりません。これは以前にUbuntu(またはDebian)で見たことがありますpippython。また、/usr/localほとんど(後で)ユーザーインストーラーにインストールされている項目に使用したくありません。

おすすめ記事