Python: ModuleNotFoundError: 找不到模块 Rich

huangapple go评论81阅读模式
英文:

Python: ModuleNotFoundError: No module named Rich

问题

I am trying to use rich module to draw table in python3.9 in Linux.
However, I am getting:

Traceback (most recent call last):
File "/opt/audit.py", line 12, in
from rich.table import Table
ModuleNotFoundError: No module named 'rich'

I have both python 2.7 and 3.9 installed.
Command I run: python3.9 /opt/audit.py

I have verified I have rich installed.
python3.9 -m pip list
Package Version


ansitable 0.9.7
colored 2.2.2
markdown-it-py 3.0.0
mdurl 0.1.2
pip 22.0.4
Pygments 2.15.1
rich 13.4.2
setuptools 58.1.0

python3.9 -m pip install rich --upgrade
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: rich in /disk2/users/python3.9/site-packages (13.4.2)
Requirement already satisfied: markdown-it-py>=2.2.0 in /disk2/users/python3.9/site-packages (from rich) (3.0.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /disk2/users/python3.9/site-packages (from rich) (2.15.1)
Requirement already satisfied: mdurl~=0.1 in /disk2/users/python3.9/site-packages (from markdown-it-py>=2.2.0->rich) (0.1.2)

This is the start of the file and it fails on line 12:

import cx_Oracle
import configparser
import json
import os
import requests
import smtplib

from rich.table import Table
from email.message import EmailMessage
from pathlib import Path

英文:

I am trying to use rich module to draw table in python3.9 in Linux.
However, I am getting:

Traceback (most recent call last):
  File &quot;/opt/audit.py&quot;, line 12, in &lt;module&gt;
    fom rich.table import Table
ModuleNotFoundError: No module named &#39;rich&#39;

I have both python 2.7 and 3.9 installed.
Command I run: python3.9 /opt/audit.py

I have verified I have rich installed. 
python3.9 -m pip list
Package        Version
-------------- -------
ansitable      0.9.7
colored        2.2.2
markdown-it-py 3.0.0
mdurl          0.1.2
pip            22.0.4
Pygments       2.15.1
rich           13.4.2
setuptools     58.1.0

python3.9 -m pip install rich --upgrade
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: rich in /disk2/users/python3.9/site-packages (13.4.2)
Requirement already satisfied: markdown-it-py&gt;=2.2.0 in /disk2/users/python3.9/site-packages (from rich) (3.0.0)
Requirement already satisfied: pygments&lt;3.0.0,&gt;=2.13.0 in /disk2/users/python3.9/site-packages (from rich) (2.15.1)
Requirement already satisfied: mdurl~=0.1 in /disk2/users/python3.9/site-packages (from markdown-it-py&gt;=2.2.0-&gt;rich) (0.1.2)

This is the start of the file and it fails on line 12:

import cx_Oracle
import configparser
import json
import os
import requests
import smtplib

from rich.table import Table
from email.message import EmailMessage
from pathlib import Path

答案1

得分: 1

如果该软件包已正确安装,有可能是由于某些配置错误,python3 未加载安装在该路径上的软件包。

您可以通过检查模块由pip安装到何处来开始故障排除:

$ python3 -m pip show rich

Name: rich
Version: 13.4.2
Summary: 渲染富文本、表格、进度条、语法高亮、Markdown等到终端
Home-page: https://github.com/Textualize/rich
Author: Will McGugan
Author-email: willmcgugan@gmail.com
License: MIT
Location: /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages
Requires: markdown-it-py, pygments
Required-by: 

如果Location路径也出现在下面的sys.path列表中,这意味着该路径是全局site-packages目录的一部分,Python应该能够在解释器运行时找到该模块:

$ python3 -m site

sys.path = [
    '/Users/Admin/workspace',
    '/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip',
    '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11',
    '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload',
    '/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages',
]
USER_BASE: '/Users/Admin/Library/Python/3.11' (不存在)
USER_SITE: '/Users/Admin/Library/Python/3.11/lib/python/site-packages' (不存在)
ENABLE_USER_SITE: True

如您在此示例中所见,Location路径是sys.path的最后一个条目。但是,如果它不在其中,有可能会在用户site-packages目录中列出:

$ python3 -m site --user-site

如果Location路径在这些site-packages中都不存在,那么您可以尝试将该路径添加到其中一个。但此时我会认真质疑您的Python安装的完整性。

英文:

If that package has been installed correctly, it is possible that python3 is not loading packages installed on that path due to some misconfiguration.

You can start troubleshooting by checking where the module was installed by pip:

$ python3 -m pip show rich

Name: rich
Version: 13.4.2
Summary: Render rich text, tables, progress bars, syntax highlighting, markdown and more to the terminal
Home-page: https://github.com/Textualize/rich
Author: Will McGugan
Author-email: willmcgugan@gmail.com
License: MIT
Location: /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages
Requires: markdown-it-py, pygments
Required-by: 

If the Location path is also present in the sys.path list below, it means that this path is part of the global site-packages directories and python should be able to find the module when the interpreter runs:

$ python3 -m site

sys.path = [
    &#39;/Users/Admin/workspace&#39;,
    &#39;/Library/Frameworks/Python.framework/Versions/3.11/lib/python311.zip&#39;,
    &#39;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11&#39;,
    &#39;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/lib-dynload&#39;,
    &#39;/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages&#39;,
]
USER_BASE: &#39;/Users/Admin/Library/Python/3.11&#39; (doesn&#39;t exist)
USER_SITE: &#39;/Users/Admin/Library/Python/3.11/lib/python/site-packages&#39; (doesn&#39;t exist)
ENABLE_USER_SITE: True

As you can see in this example, the Location path is the last entry on sys.path. However, if it's not in there, there's a chance it might be listed by the user site-package directories:

$ python3 -m site --user-site

If the Location path is not present in either of these site-packages, then you can try to add that path to one of them. But at this point I would seriously question the integrity of your python installation.

huangapple
  • 本文由 发表于 2023年6月26日 23:46:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76558264.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定