python 模块存储在哪里?

发布于 2024-09-03 11:13:47 字数 186 浏览 2 评论 0 原文

我最近开始学习Python,有两个与模块相关的问题。

  1. 有没有办法获取机器上可用(即安装)的Python模块列表?
  2. 我使用 Ubuntu Karmic 和 Synaptic 进行包管理。我刚刚安装了一个 python 模块。模块代码实际存储在我的机器上的哪里? (是否有默认的[推荐]模块存储位置)?

I have recently started learning Python and I have 2 questions relating to modules.

  1. Is there a way to obtain a list of Python modules available (i.e. installed) on a machine?
  2. I am using Ubuntu Karmic and Synaptic for package management. I have just installed a python module.Where is the module code actually stored on my machine? (is there a default [recommended] location that modules are stored)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

命比纸薄 2024-09-10 11:13:47
  1. 有没有办法获得列表
    可用的Python模块(即
    安装)在机器上?

这对我有用:

help('modules')
  • 模块代码实际上在哪里
    存储在我的机器上?
  • 通常位于 Python 文件夹中的 /lib/site-packages 中。 (至少在 Windows 上。)

    您可以使用 sys.path 来了解在哪些目录中搜索模块。

    1. Is there a way to obtain a list of
      Python modules available (i.e.
      installed) on a machine?

    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. (At least, on Windows.)

    You can use sys.path to find out what directories are searched for modules.

    强者自强 2024-09-10 11:13:47

    在 python 命令行上,首先导入您需要位置的模块。

    import module_name
    

    然后输入:

    print(module_name.__file__)
    

    例如找出“pygal”位置:

    import pygal
    print(pygal.__file__)
    

    输出:

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

    On python command line, first import that module for which you need location.

    import module_name
    

    Then type:

    print(module_name.__file__)
    

    For example to find out "pygal" location:

    import pygal
    print(pygal.__file__)
    

    Output:

    /anaconda3/lib/python3.7/site-packages/pygal/__init__.py
    
    耀眼的星火 2024-09-10 11:13:47

    如果您使用的是pip

    pip show <package name>
    

    pip showtensorflow的示例输出:

    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: [email protected]
    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
    

    安装位置显示在Location:/home/user/.local/lib/python3。 6/站点包

    If you are using pip:

    pip show <package name>
    

    Sample output of 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: [email protected]
    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.

    蓝色星空 2024-09-10 11:13:47

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

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

    On Windows machine python modules are located at (system drive and python version may vary):

    C:\Users\Administrator\AppData\Local\Programs\Python\Python38\Lib
    
    岁月静好 2024-09-10 11:13:47

    您可以通过首先列出模块来找到模块​​代码:

    help("modules")
    

    这将显示 Python 可以导入的模块列表。在这个列表的底部有一个短语:

    输入任何模块名称以获得更多帮助。或者,输入“modules spam”
    搜索名称或摘要包含字符串“spam”的模块。

    查找模块位置:

    help("module_Name")
    

    例如:

    help("signal")
    

    这里有很多信息。滚动到底部找到其位置

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

    复制链接。退出 Python REPL 后查看代码:

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

    You can find module code by first listing the modules:

    help("modules")
    

    This spits out a list of modules Python can import. 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".

    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:

    nano /usr/lib/python3.5/signal.py
    
    梦途 2024-09-10 11:13:47

    运行 python CLI 获取信息

    python -c "import sys; print('\n'.join(sys.path))"
    

    Run python CLI for info

    python -c "import sys; print('\n'.join(sys.path))"
    
    瞳孔里扚悲伤 2024-09-10 11:13:47
    1. 您可以迭代 sys.path 查找所有模块(内置模块除外)。
    2. 它可能位于 /usr/lib/pythonX.X/site-packages 附近(再次参见 sys.path)。并考虑使用本机 Python 包管理(通过 pipeasy_install,加上 yolk)相反,Linux 发行版维护的存储库中的包往往会过时。
    1. You can iterate through directories listed in sys.path to find all modules (except builtin ones).
    2. It'll probably be somewhere around /usr/lib/pythonX.X/site-packages (again, see 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.
    薄荷→糖丶微凉 2024-09-10 11:13:47

    1) 使用帮助功能

    进入 python 提示符并键入以下命令:

    >>>help("modules")
    

    这将列出系统中安装的所有模块。您不需要安装任何额外的包来列出它们,但您需要从列表中手动搜索或过滤所需的模块。

    2) 使用 pip freeze

    sudo apt-get install python-pip
    pip freeze
    

    即使您需要安装额外的软件包才能使用此方法,但此方法允许您使用 grep 命令轻松搜索或过滤结果。例如:pip freeze | grep 提要。

    您可以使用任何您方便的方法。

    1) Using the help function

    Get into the python prompt and type the following command:

    >>>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

    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. e.g. pip freeze | grep feed.

    You can use whichever method is convenient for you.

    月寒剑心 2024-09-10 11:13:47

    如果您使用 condapip 安装模块,您可以使用

    pip list
    

    conda list
    

    显示所有模块。这将显示终端本身的所有模块,并且比

    >>> help('modules')
    
    

    If you are using conda or pip to install modules you can use

    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')
    
    
    雨巷深深 2024-09-10 11:13:47

    在默认安装了 Python 3.8 或更高版本的 2023 Windows 计算机上:

    按 Windows 键,粘贴或键入以下内容,然后按 Enter

    explorer %LOCALAPPDATA%\Programs\Python
    

    这将打开 %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

    explorer %LOCALAPPDATA%\Programs\Python
    

    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.

    悍妇囚夫 2024-09-10 11:13:47

    在 Linux 上,使用 grep 查找所选模块,无需额外安装,很快就能完成。

    -r 代表在子目录中递归搜索,l 代表仅显示文件,而不显示目录。通常您可以从即将出现的列表中看到位置,并且可以使用 Ctrl-C 停止输出。

    grep -rl module_name_or_part_of_name /
    

    或者,借用此用户的值评论:

    pip list | grep module_name_or_part_of_name
    

    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.

    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
    
    故乡的云 2024-09-10 11:13:47

    在我的本地计算机(Win 10)上,它具有以下路径:

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

    On my local machine (Win 10), it has the following path:

    c:\Users\administrator\AppData\Roaming\Python\Python38\
    
    九歌凝 2024-09-10 11:13:47

    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 to sys.path. It contains info that help('module') doesn't.

    meta_path can explain unexpected import behaviors - if a module isn't on sys.path, it doesn't guarantee it won't be imported. Moreover, if a package is on sys.path, but a package with same name but different directory is in sys.meta_path, Python will import from both, prioritizing sys.path. This can be troublesome if you've intentionally removed something from the package in sys.path - Python will keep importing it from sys.meta_path. This includes certain non-Python files, like Cython's .pyx builds.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文