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)에서 본 적이 있는 pip것이지만 python. 또한 /usr/local대부분 (나중에) 사용자 설치인 에 설치된 항목에 사용되는 것을 원하지 않습니다 .

관련 정보