已安装所需模块lxml
,但导入时出现错误ImportError: No module named lxml
我怎么知道
- 从哪个位置考虑 python 模块?
- 正在使用或设置哪个 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
,这些东西主要是(后来的)用户安装。