python 模块存储在哪里?
我最近开始学习Python,有两个与模块相关的问题。
- 有没有办法获取机器上可用(即安装)的Python模块列表?
- 我使用 Ubuntu Karmic 和 Synaptic 进行包管理。我刚刚安装了一个 python 模块。模块代码实际存储在我的机器上的哪里? (是否有默认的[推荐]模块存储位置)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
这对我有用:
通常位于 Python 文件夹中的
/lib/site-packages
中。 (至少在 Windows 上。)您可以使用
sys.path
来了解在哪些目录中搜索模块。
This works for me:
Usually in
/lib/site-packages
in your Python folder. (At least, on Windows.)You can use
sys.path
to find out what directories are searched for modules.在 python 命令行上,首先导入您需要位置的模块。
然后输入:
例如找出“pygal”位置:
输出:
On python command line, first import that module for which you need location.
Then type:
For example to find out "pygal" location:
Output:
如果您使用的是
pip
:pip showtensorflow
的示例输出:安装位置显示在
Location:/home/user/.local/lib/python3。 6/站点包
。If you are using
pip
:Sample output of
pip show tensorflow
:The installed location is shown at
Location:/home/user/.local/lib/python3.6/site-packages
.在 Windows 机器上,Python 模块位于(系统驱动器和 Python 版本可能有所不同):
On Windows machine python modules are located at (system drive and python version may vary):
您可以通过首先列出模块来找到模块代码:
这将显示 Python 可以导入的模块列表。在这个列表的底部有一个短语:
查找模块位置:
例如:
这里有很多信息。滚动到底部找到其位置
复制链接。退出 Python REPL 后查看代码:
You can find module code by first listing the modules:
This spits out a list of modules Python can import. At the bottom of this list is a phrase:
To find module location:
for example:
A lot of information here. Scroll to the bottom to find its location
Copy link. To see code, after exiting Python REPL:
运行 python CLI 获取信息
Run python CLI for info
sys.path
查找所有模块(内置模块除外)。/usr/lib/pythonX.X/site-packages
附近(再次参见sys.path
)。并考虑使用本机 Python 包管理(通过pip
或easy_install
,加上yolk
)相反,Linux 发行版维护的存储库中的包往往会过时。sys.path
to find all modules (except builtin ones)./usr/lib/pythonX.X/site-packages
(again, seesys.path
). And consider using native Python package management (viapip
oreasy_install
, plusyolk
) instead, packages in Linux distros-maintained repositories tend to be outdated.1) 使用帮助功能
进入 python 提示符并键入以下命令:
这将列出系统中安装的所有模块。您不需要安装任何额外的包来列出它们,但您需要从列表中手动搜索或过滤所需的模块。
2) 使用 pip freeze
即使您需要安装额外的软件包才能使用此方法,但此方法允许您使用
grep
命令轻松搜索或过滤结果。例如:pip freeze | grep 提要。您可以使用任何您方便的方法。
1) Using the help function
Get into the python prompt and type the following command:
This will list all the modules installed in the system. You don't need to install any additional packages to list them, but you need to manually search or filter the required module from the list.
2) Using pip freeze
Even though you need to install additional packages to use this, this method allows you to easily search or filter the result with
grep
command. e.g.pip freeze | grep feed
.You can use whichever method is convenient for you.
如果您使用
conda
或pip
安装模块,您可以使用或
显示所有模块。这将显示终端本身的所有模块,并且比
If you are using
conda
orpip
to install modules you can useor
to display all the modules. This will display all the modules in the terminal itself and is much faster than
在默认安装了 Python 3.8 或更高版本的 2023 Windows 计算机上:
按 Windows 键,粘贴或键入以下内容,然后按 Enter
这将打开
%LOCALAPPDATA%\Programs\Python
,其中包含按各种名称命名的目录安装的 Python 版本(也许是过去)及其各自安装的软件包。On a 2023 Windows machine with Python 3.8 or later installed with defaults:
Press the Windows key, paste or type the following and press enter
This opens
%LOCALAPPDATA%\Programs\Python
which contains directories named per the various versions of Python installed (perhaps, in the past), and their respective installed packages.在 Linux 上,使用 grep 查找所选模块,无需额外安装,很快就能完成。
-r
代表在子目录中递归搜索,l 代表仅显示文件,而不显示目录。通常您可以从即将出现的列表中看到位置,并且可以使用 Ctrl-C 停止输出。或者,借用此用户的值评论:
On Linux, use grep to find a chosen module, no extra installation needed for that, quickly done.
The
-r
stands for recursive search in the sub-directories and the l to show only the files, not the directories. Usually you can see the locations from the upcoming list, and you can stop the output with Ctrl-C.or, borrowed from the value comment here from this user:
在我的本地计算机(Win 10)上,它具有以下路径:
On my local machine (Win 10), it has the following path:
sys.meta_path
是除了sys.path
之外的另一个模块源。它包含help('module')
不包含的信息。meta_path
可以解释意外的导入行为 - 如果模块不在sys.path
上,则不能保证它不会被导入。此外,如果sys.path上有一个包,但sys.meta_path
中有一个同名但不同目录的包,Python将导入从两者中,优先考虑sys.path
。如果您故意从sys.path
中的包中删除某些内容,这可能会很麻烦 - Python 将继续从sys.meta_path
导入它。这包括某些非 Python 文件,例如 Cython 的 .pyx 构建。sys.meta_path
is another source of modules, in addition tosys.path
. It contains info thathelp('module')
doesn't.meta_path
can explain unexpected import behaviors - if a module isn't onsys.path
, it doesn't guarantee it won't be imported. Moreover, if a package is onsys.path
, but a package with same name but different directory is insys.meta_path
, Python will import from both, prioritizingsys.path
. This can be troublesome if you've intentionally removed something from the package insys.path
- Python will keep importing it fromsys.meta_path
. This includes certain non-Python files, like Cython's.pyx
builds.