简体   繁体   English

如果我将一个包导入到 python 中,我是否还必须单独重视它的模块?

[英]If I import a package into python, do I also have to important it's modules separately?

Very simply, I'm reasonably new to coding, and I'm going through another person's code to understand what it is doing as I have to use it for data analysis but I notice that they do the following:很简单,我对编码相当陌生,我正在浏览另一个人的代码以了解它在做什么,因为我必须使用它进行数据分析,但我注意到他们执行以下操作:

 import matplotlib.pyplot as plt
.
.
.
import matplotlib as mpl
import numpy as np
.
.
import matplotlib.ticker

I thought that " import matplotlib as mpl " would import all modules contained in matplotlib and therefore there need to separately import the module " ticker " from matplotlib after this?我认为“ import matplotlib as mpl ”会导入matplotlib中包含的所有模块,因此在此之后需要从matplotlib单独导入模块“ ticker ”? I would have thought that they could just use "mpl.ticker" later on and it would work?我原以为他们可以稍后使用"mpl.ticker" ,它会起作用吗?

Why would this be the case?为什么会这样?

import matplotlib as mpl

Yes, this imports every top level function and class from the matplotlib package (and makes them accessible under the namespace mpl ), but it does not import any submodules it has .是的,这会从matplotlib包中导入每个顶级函数和类(并使它们可以在命名空间mpl下访问),但它不会导入它拥有的任何子模块

pyplot is a module in the matplotlib package. pyplotmatplotlib包中的一个模块。 If one needs access to classes/functions from pyplot , it has to imported also:如果需要从pyplot访问类/函数,它还必须导入:

import matplotlib.pyplot as plt

For the same reason you have to import matplotlib.ticker to be able to use stuff from that module with mpl.ticker.Foo .出于同样的原因,您必须import matplotlib.ticker才能通过mpl.ticker.Foo使用该模块中的mpl.ticker.Foo

Here's a quick demo showing that just importing the base matplotlib package is not enough:这是一个快速演示,显示仅导入基本matplotlib包是不够的:

>>> import matplotlib as mpl
>>> mpl.pyplot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'matplotlib' has no attribute 'pyplot'
>>> mpl.ticker
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'matplotlib' has no attribute 'ticker'
>>> import matplotlib.pyplot as plt
>>> plt.plot
<function plot at 0x0EF21198>
>>> import matplotlib.ticker
>>> mpl.ticker.Locator
<class 'matplotlib.ticker.Locator'>

暂无
暂无

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

相关问题 可安装的distutils软件包:如何从也是同一软件包一部分的命令行脚本中导入软件包模块? - Installable distutils packages: How do I import package modules from command line scripts which are also part of the same package? 我有一系列要放入包装中的python模块,我该怎么做? - I have a series of python modules I would like to put into a package, how can I do this? 如何在不导入函数的情况下将函数导入Python包中? - How do I import a function into a Python package without also importing the file that contains it? 如何找到可从 package 中导入的模块? - How do I find the modules that are available for import from within a package? 我如何控制软件包中的模块以导入python? - how can i control modules in package to import in python? 当用户的系统可能还安装了 Python 2.7 时,如何在用户系统上运行 Python 3 脚本? - How do I run a Python 3 script on a user's system when they may also have Python 2.7 installed? 我如何在python中正确导入一个包? - how do i correctly import a package in python? 如何在 python 中导入“umap”package? - How do I import 'umap' package in python? 如何安装导入numpy、pandas等模块? 我想使用IDLE编辑/运行环境,安装了Python 3.10 - How do I install and import modules such as numpy and pandas? I want to use the IDLE editing/run environment, and I have Python 3.10 installed 我在将模块从 github 导入到 python 时遇到问题 - I have a problem with the import of modules from github to python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM