简体   繁体   English

python个模块存放在哪里?

[英]Where are the python modules stored?

I have recently started learning Python and I have 2 questions relating to modules.我最近开始学习 Python,我有 2 个与模块相关的问题。

  1. Is there a way to obtain a list of Python modules available (ie installed) on a machine?有没有办法获取机器上可用(即已安装)的 Python 个模块的列表?
  2. I am using Ubuntu Karmic and Synaptic for package management.我正在使用 Ubuntu Karmic 和 Synaptic 进行 package 管理。 I have just installed a python module.Where is the module code actually stored on my machine?我刚刚安装了一个 python 模块。模块代码实际存储在我的机器上的什么位置? (is there a default [recommended] location that modules are stored)? (是否有存储模块的默认 [推荐] 位置)?
  1. Is there a way to obtain a list of Python modules available (ie installed) on a machine?有没有办法获取机器上可用(即安装)的 Python 模块列表?

This works for me:这对我有用:

help('modules')
  1. Where is the module code actually stored on my machine?我的机器上实际存储的模块代码在哪里?

Usually in /lib/site-packages in your Python folder.通常在 Python 文件夹中的/lib/site-packages中。 (At least, on Windows.) (至少,在 Windows 上。)

You can use sys.path to find out what directories are searched for modules.您可以使用sys.path找出在哪些目录中搜索模块。

On python command line, first import that module for which you need location.在 python 命令行上,首先导入需要位置的模块。

import module_name

Then type:然后输入:

print(module_name.__file__)

For example to find out "pygal" location:例如找出“pygal”位置:

import pygal
print(pygal.__file__)

Output:输出:

/anaconda3/lib/python3.7/site-packages/pygal/__init__.py

在 Windows 机器上,python 模块位于(系统驱动器和 python 版本可能会有所不同):

C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib

You can find module code by first listing the modules:您可以通过首先列出模块来找到模块​​代码:

help("modules")

This spits out a list of modules Python can import.这会输出 Python 可以导入的模块列表。 At the bottom of this list is a phrase:在这个列表的底部是一个短语:

Enter any module name to get more help.输入任何模块名称以获取更多帮助。 Or, type "modules spam" to search for modules whose name or summary contain the string "spam".或者,键入“modules spam”以搜索名称或摘要中包含字符串“spam”的模块。

To find module location:查找模块位置:

help("module_Name")

for example:例如:

help("signal")

A lot of information here.这里有很多信息。 Scroll to the bottom to find its location滚动到底部以找到它的位置

/usr/lib/python3.5/signal.py

Copy link.复制链接。 To see code, after exiting Python REPL:要查看代码,请在退出 Python REPL 后:

nano /usr/lib/python3.5/signal.py
  1. You can iterate through directories listed in sys.path to find all modules (except builtin ones).您可以遍历sys.path列出的目录以查找所有模块(内置模块除外)。
  2. It'll probably be somewhere around /usr/lib/pythonX.X/site-packages (again, see sys.path ).它可能在/usr/lib/pythonX.X/site-packages附近(再次参见sys.path )。 And consider using native Python package management (via pip or easy_install , plus yolk ) instead, packages in Linux distros-maintained repositories tend to be outdated.并考虑使用原生 Python 包管理(通过pipeasy_install ,加上yolk ),Linux 发行版维护的存储库中的包往往已经过时。

If you are using conda or pip to install modules you can use如果您使用condapip安装模块,您可以使用

pip list

or或者

conda list

to display all the modules.显示所有模块。 This will display all the modules in the terminal itself and is much faster than这将显示终端本身中的所有模块,并且比

>>> help('modules')

1) Using the help function 1) 使用帮助功能

Get into the python prompt and type the following command:进入 python 提示符并键入以下命令:

>>>help("modules")

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 2)使用pip冻结

sudo apt-get install python-pip
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.即使您需要安装其他软件包才能使用它,此方法也允许您使用grep命令轻松搜索或过滤结果。 eg pip freeze | grep feed例如pip freeze | grep feed pip freeze | grep feed . pip freeze | grep feed

You can use whichever method is convenient for you.您可以使用任何您方便的方法。

If you are using pip :如果您使用pip

pip show <package name>

Sample output of pip show tensorflow : pip show tensorflow示例输出:

Name: tensorflow
Version: 2.1.1
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /home/user/.local/lib/python3.6/site-packages
Requires: termcolor, six, astor, numpy, grpcio, absl-py, protobuf, tensorflow-estimator, tensorboard, gast, keras-applications, opt-einsum, wheel, keras-preprocessing, google-pasta, scipy, wrapt
Required-by: tf-models-official

The installed location is shown at Location:/home/user/.local/lib/python3.6/site-packages .安装位置显示在Location:/home/user/.local/lib/python3.6/site-packages

On Linux, use grep to find a chosen module, no extra installation needed for that, quickly done.在 Linux 上,使用 grep 查找所选模块,无需额外安装,快速完成。

The -r stands for recursive search in the sub-directories and the l to show only the files, not the directories. -r代表在子目录中递归搜索,l 代表仅显示文件,而不显示目录。 Usually you can see the locations from the upcoming list, and you can stop the output with Ctrl-C.通常您可以从即将到来的列表中看到位置,您可以使用 Ctrl-C 停止输出。

grep -rl module_name_or_part_of_name /

or, borrowed from the value comment here from this user :或者,从这个用户的价值评论中借用:

pip list | grep module_name_or_part_of_name

On my local machine (Win 10), it has the following path:在我的本地计算机 (Win 10) 上,它具有以下路径:

c:\Users\administrator\AppData\Roaming\Python\Python38\

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM