Python笔记 #05# Package & pip3

datacamp + 日常收集

How to install Package 

pip3 & What is difference between pip and pip3?

Import  Package

Selective import

Different ways of importing

How to install Package 

在 Python 中,我们通常使用 pip 来安装 Package(扩展包)。pip 是 python 的一个包管理工具(类似于各种 Linux distribution 里的包管理工具)。

步骤是这样的,首先需要先到 pip 的官网(google 一下)下载 get-pip.py ,之后打开终端运行 get-pip.py “真正”地安装它,然后就可以轻松地使用 pip 命令来安装各种扩展包。

What is difference between pip and pip3?

pip3 always operates on the Python3 environment only, as pip2 does with Python2. pip operates on whichever environment is appropriate to the context. For example if you are in a Python3 venv, pip will operate on the Python3 environment. -- Brendan Donegan, Automating testing using Python for 6 years

Import Package

当我们安装好扩展包后,如何在代码中使用它呢?主要有以下几种方式:

1)

2)

3)

 用哪种全凭喜好,不过我比较倾向于用方式一。范例代码:

# Definition of radius
r = 0.43

# Import the math package
import math

# Calculate C
C = 2 * math.pi * r

# Calculate A
A = math.pi * r ** 2

# Build printout
print("Circumference: " + str(C))
print("Area: " + str(A))

Selective import

# Definition of radius
r = 192500

# Import radians function of math package
from math import radians

# Travel distance of Moon over 12 degrees. Store in dist.
dist = r * radians(12)

# Print out dist
print(dist)

Different ways of importing

There are several ways to import packages and modules into Python. Depending on the import call, you'll have to use different Python code.

Suppose you want to use the function inv(), which is in the linalgsubpackage of the scipy package. You want to be able to use this function as follows:

my_inv([[1,2], [3,4]])

Which import statement will you need in order to run the above code without an error?

from scipy.linalg import inv as my_inv
原文地址:https://www.cnblogs.com/xkxf/p/8254205.html