Python虚拟环境中成功安装包后仍触发ModuleNotFoundError的问题求助
Python虚拟环境中成功安装包后仍触发ModuleNotFoundError的问题求助
我在运行Linaro内核的嵌入式计算机上搭建了Python虚拟环境,但在虚拟环境里用pip安装任何包后,都会出现ModuleNotFoundError: No module named 'x'的错误;但如果在虚拟环境外安装包,一切都正常。
背景情况
- 最开始我是用
python3 -m ensurepip --default-pip安装的pip,版本是22.2.2。后来尝试用pip install --upgrade pip升级到24.3.1,虽然提示安装成功,但出现了警告:
WARNING: The scripts pip, pip3 and pip3.10 are installed in '/var/user-packages/data/user_storage/ot2-dil-ali/bin' which is not on PATH.
- 我把这个目录加到了
$PATH里,但之后用pip安装包时还是会提示no module named pip,无奈只能重新运行最初的命令装回了22.2.2版本的pip。另外我在home目录和/etc目录里都找不到.profile或.bashrc文件。
环境路径检查结果
运行which python3 pip的结果如下:
(myvenv) # which python3 pip /data/user_storage/ot2-dil-ali/bin/python3 /data/user_storage/ot2-dil-ali/bin/pip # which python3 pip /usr/bin/python3 /usr/bin/pip
安装包后的异常表现
在虚拟环境里安装包时,明明显示安装成功:
(myvenv) # python3 -m pip install loguru Collecting loguru Using cached loguru-0.7.3-py3-none-any.whl (61 kB) Installing collected packages: loguru Successfully installed loguru-0.7.3
但尝试导入时会触发ModuleNotFoundError,而且虚拟环境的site-packages目录里根本找不到这个包:
(myvenv) # ls myvenv/lib/python3.10/site-packages/ _distutils_hack pip-22.2.2.dist-info setuptools-63.2.0.dist-info distutils-precedence.pth pkg_resources pip setuptools
后续排查尝试
Edit 1: 指定虚拟环境内的pip安装仍无效
即使直接用虚拟环境目录下的pip安装,问题依旧:
(myvenv) # myvenv/bin/pip install loguru Collecting loguru Using cached loguru-0.7.3-py3-none-any.whl (61 kB) Installing collected packages: loguru Successfully installed loguru-0.7.3 [notice] A new release of pip available: 22.2.2 -> 24.3.1 [notice] To update, run: pip install --upgrade pip (myvenv) # myvenv/bin/python3 ... >>> import loguru Traceback (most recent call last): File "<stdin>", line 1, in <module> ModuleNotFoundError: No module named 'loguru' >>>
Edit 2: 详细安装日志
用带详细日志的命令安装,日志显示pip确实在处理安装,但结果还是一样:
(myvenv) # myvenv/bin/pip3 install loguru -v -v -v Using pip 22.2.2 from /data/user_storage/myvenv/lib/python3.10/site-packages/pip (python 3.10) Non-user install because user site-packages disabled Created temporary directory: /tmp/pip-ephem-wheel-cache-mw5uucuc Created temporary directory: /tmp/pip-build-tracker-ya1d5lt3 Initialized build tracking at /tmp/pip-build-tracker-ya1d5lt3 Created build tracker: /tmp/pip-build-tracker-ya1d5lt3 Entered build tracker: /tmp/pip-build-tracker-ya1d5lt3 Created temporary directory: /tmp/pip-install-r6fhmj4u 1 location(s) to search for versions of loguru: * https://pypi.org/simple/loguru/ Fetching project page and analyzing links: https://pypi.org/simple/loguru/ Getting page https://pypi.org/simple/loguru/ Found index url https://pypi.org/simple Looking up "https://pypi.org/simple/loguru/" in the cache Request header has "max_age" as 0, cache bypassed Starting new HTTPS connection (1): pypi.org:443 https://pypi.org:443 "GET /simple/loguru/ HTTP/1.1" 304 0 Fetched page https://pypi.org/simple/loguru/ as application/vnd.pypi.simple.v1+json Found link https://files.pythonhosted.org/packages/01/85/1e0e7b10552b6c0c736e5d288a5c0e650e340d78b4a55b7e04d081db3f87/loguru-0.1.0-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.1.0 Found link https://files.pythonhosted.org/packages/63/69/3e9ca8cf8da389156838b0599a895cf977b26e1cc43677359e8522b81e19/loguru-0.1.0.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.1.0 Found link https://files.pythonhosted.org/packages/5e/e2/8814a383f90b07c9b8afc7ecf8fa7ab867104ee19759d30d7d8652305797/loguru-0.2.0-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.2.0 Found link https://files.pythonhosted.org/packages/e0/0d/cc26b167a3a64b047e623d3e1ed674cdf9f8bddd9c1cf9b467a223f75417/loguru-0.2.0.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.2.0 Found link https://files.pythonhosted.org/packages/26/b5/539056e10c4fc7d7fd1a09cf3f45358c2675a0abcf54bbde9009fa4d4f73/loguru-0.2.1-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.2.1 Found link https://files.pythonhosted.org/packages/aa/de/1da30e97385dc8233d336ac6b0f47ea04b4a8bc87dde3477139909d4c7b0/loguru-0.2.1.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.2.1 Found link https://files.pythonhosted.org/packages/59/c9/9e5826a7d7be659ddfec35ca890ee818c2a51e8d2c0977fe60d1950633de/loguru-0.2.2-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.2.2 Found link https://files.pythonhosted.org/packages/5c/e3/b739229d5e255d6428fac81d57131ed3f8519e2b9d68421ed9a69ed78eb6/loguru-0.2.2.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.6), version: 0.2.2 Found link https://files.pythonhosted.org/packages/74/5b/8cce2218c5f89765fcbf73a2275231512f12d185897841194d4f4079c381/loguru-0.2.3-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.2.3 Found link https://files.pythonhosted.org/packages/96/f6/7dfcd8f6594f1571c2297dbb0b3937daeaa8daeeebbfdceb7d73af176a46/loguru-0.2.3.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.2.3 Found link https://files.pythonhosted.org/packages/55/e0/461d6e5427c90fea5fcc72e364a5e742f8c1c306622685ec61b1e93a798e/loguru-0.2.4-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.2.4 Found link https://files.pythonhosted.org/packages/bf/c8/6aebe119baccac526baa0db465e31cac8095ef9363935b5035c9dde01eab/loguru-0.2.4.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.2.4 Found link https://files.pythonhosted.org/packages/c4/2d/2861600f1abed3c85e157c78308d3b1de974ad64d67de852a79da9ae7205/loguru-0.2.5-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.2.5 Found link https://files.pythonhosted.org/packages/d0/de/79e8314cb7473289010502b7bd352117b189225979e5d5e512fe2ada7633/loguru-0.2.5.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.2.5 Found link https://files.pythonhosted.org/packages/4a/59/9deeeba62ecfcb771c0e76d63fab565e2c229e1e418d0c410d9313fa7a4c/loguru-0.3.0-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.3.0 Found link https://files.pythonhosted.org/packages/f6/af/090f0a4eddafe1894edb0140c903b83c0e95d6f5e50885baed025a1fc696/loguru-0.3.0.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.3.0 Found link https://files.pythonhosted.org/packages/c7/eb/1bc16bba5103cf863cb2984dca9ac401aaf29f0f04142004534f1fa98f09/loguru-0.3.1-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.3.1 Found link https://files.pythonhosted.org/packages/2b/99/7551cfff9a7a9b06599b51ce4d4ea16866e5c82d4cd2bbdb6f0867c41adb/loguru-0.3.1.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.3.1 Found link https://files.pythonhosted.org/packages/d5/db/669c5927e31c47c1cf5beef7ee784cc5fe88f95f47a86c5814a4f3de45f6/loguru-0.3.2-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.3.2 Found link https://files.pythonhosted.org/packages/5c/de/42db5a4bf0f02786a54ed34b676536499c74bc09540c60d70022652d1c5b/loguru-0.3.2.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.3.2 Found link https://files.pythonhosted.org/packages/57/dd/be19f64691d250bbd98906254307abd626dbbd674b019a313f57d6338bc7/loguru-0.4.0-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.4.0 Found link https://files.pythonhosted.org/packages/1d/85/eb26882a550144b5ae95cd2be1d24794969817fbcca6a66db8089b37df3b/loguru-0.4.0.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.4.0 Found link https://files.pythonhosted.org/packages/b2/f4/2c8b94025c6e30bdb331c7ee628dc152771845aedff35f0365c2a4dacd42/loguru-0.4.1-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.4.1 Found link https://files.pythonhosted.org/packages/b8/b7/ec749f8f95454d6043900f99d8e6ec48be89f62d861e909c3eb0407c54a7/loguru-0.4.1.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.4.1 Found link https://files.pythonhosted.org/packages/80/b0/4413a201fcdcdc6789050c536d3b4ece601975ded9e0d676ef47f582348d/loguru-0.5.0-py3-none-any.whl (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.5.0 Found link https://files.pythonhosted.org/packages/7b/ae/a0e2d02fb46e5bb23a7e58f7bdace699f8173eb0271909b4bc4d90f95460/loguru-0.5.0.tar.gz (from https://pypi.org/simple/loguru/) (requires-python:>=3.5), version: 0.5.0 Found link https://files.pythonhosted.org/packages
相关产品推荐
相关产品推荐

