cmd匯入python模組 Python模組匯入

2021-10-11 04:38:11 字數 3132 閱讀 2235

python模組匯入

import

想使用python原始檔,只需在另乙個原始檔裡執行import語句,語法如下:

importnumpy

n=numpy.array([[1,2],[3,4]])

print(n)[[1 2]

[3 4]]

importmatplotlib.pyplotasplt

plt.plot(n)

plt.show()

from...import*

這提供了乙個簡單的方法來匯入乙個模組中的所有專案。然而這種宣告不該被過多地使用。

把乙個模組的所有內容全都匯入到當前的命名空間也是可行的,只需使用如下宣告:

fromdatetimeimport*

from...import語句:

python的from語句讓你從模組中匯入乙個指定的部分到當前命名空間中。

dir()函式乙個排好序的字串列表,返回的列表容納了在乙個模組裡定義的所有模組,變數和函式。

fromdatetimeimporttime

print(dir(time))['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'dst', 'fold', 'hour', 'isoformat', 'max', 'microsecond', 'min', 'minute', 'replace', 'resolution', 'second', 'strftime', 'tzinfo', 'tzname', 'utcoffset']

print(help(time))help on class time in module datetime:

class time(builtins.object)

| time([hour[, minute[, second[, microsecond[, tzinfo]]]]]) --> a time object

| all arguments are optional. tzinfo may be none, or an instance of

| a tzinfo subclass. the remaining arguments may be ints.

| methods defined here:

| __eq__(self, value, /)

| return self==value.

| __format__(...)

| formats self with strftime.

| __ge__(self, value, /)

| return self>=value.

| __getattribute__(self, name, /)

| return getattr(self, name).

| __gt__(self, value, /)

| return self>value.

| __hash__(self, /)

| return hash(self).

| __le__(self, value, /)

| return self<=value.

| __lt__(self, value, /)

| return self

| __ne__(self, value, /)

| return self!=value.

| __new__(*args, **kwargs) from builtins.type

| create and return a new object. see help(type) for accurate signature.

| __reduce__(...)

| __reduce__() -> (cls, state)

| __reduce_ex__(...)

| __reduce_ex__(proto) -> (cls, state)

| __repr__(self, /)

| return repr(self).

| __str__(self, /)

| return str(self).

| dst(...)

| return self.tzinfo.dst(self).

| isoformat(...)

| return string in iso 8601 format, [hh[:mm[:ss[.mmm[uuu]]]]][+hh:mm].

| timespec specifies what components of the time to include.

| replace(...)

| return time with new specified fields.

| strftime(...)

| format -> strftime() style string.

| tzname(...)

| return self.tzinfo.tzname(self).

| utcoffset(...)

| return self.tzinfo.utcoffset(self).

| data descriptors defined here:

| fold

| hour

| microsecond

| minute

| second

| tzinfo

| data and other attributes defined here:

| max = datetime.time(23, 59, 59, 999999)

| min = datetime.time(0, 0)

| resolution = datetime.timedelta(0, 0, 1)

none

py 模組匯入

常用標準庫 標準庫builtins 內建函式預設載入 math 數學庫 random 隨機數 time 時間 datetime 日期和時間 calendar 日曆 hashlib 加密演算法 copy 拷貝 functools 常用工具 os 作業系統介面 re 字串正則匹配 sys pthon 自...

python 匯入模組

最近開始學習python,遇到一些匯入模組的問題,花了幾分鐘終於琢磨明白了,給初學者介紹幾種型別 一 test sys test1 nv1.py nv2.py nv1.py 如下 classdog defadd self a,b self.a a self.b b c self.a self.b r...

python匯入模組

1 模組的定義 模組定義 用來邏輯上組織python 變數 函式 類 邏輯 目的是 實現乙個功能 本質就是.py結尾的python檔案。補充 包的定義 用來從邏輯組織模組的,本質就是乙個目錄 必須帶有乙個 init py檔案 2 匯入方法 匯入模組的方法 1.import module name 匯...